forked from intoli/intoli-article-materials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselenium-mitmproxy.py
executable file
·56 lines (44 loc) · 2.04 KB
/
selenium-mitmproxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
from selenium import webdriver
# The proxy settings.
proxy_host = 'localhost'
proxy_port = 8080
# Loop through the four different configurations.
for browser in ['chrome', 'firefox']:
for mode in ['headless', 'graphical']:
# Set up the driver with the appropriate settings.
if browser == 'chrome':
# Enable headless mode.
options = webdriver.ChromeOptions()
if mode == 'headless':
options.add_argument('--headless')
# Specify the proxy.
options.add_argument('--proxy-server=%s:%s' % (proxy_host, proxy_port))
# Launch Chrome.
driver = webdriver.Chrome(chrome_options=options)
elif browser == 'firefox':
# Enable headless mode.
if mode == 'headless':
os.environ['MOZ_HEADLESS'] = '1'
elif mode == 'graphical':
os.environ.pop('MOZ_HEADLESS', None)
firefox_profile = webdriver.FirefoxProfile()
# Specify to use manual proxy configuration.
firefox_profile.set_preference('network.proxy.type', 1)
# Set the host/port.
firefox_profile.set_preference('network.proxy.http', proxy_host)
firefox_profile.set_preference('network.proxy.https_port', proxy_port)
firefox_profile.set_preference('network.proxy.ssl', proxy_host)
firefox_profile.set_preference('network.proxy.ssl_port', proxy_port)
# Launch Firefox.
driver = webdriver.Firefox(firefox_profile=firefox_profile)
# Navigate to the test page and inject the JavaScript.
driver.get('https://intoli.com/blog/javascript-injection/test-page.html')
# Save the results as an image.
os.makedirs('img', exist_ok=True)
filename = os.path.join('img',
f'selenium-mitmproxy-{browser}-{mode}-results.png')
driver.get_screenshot_as_file(filename)
print(f'Saved "{filename}".')
# Cleanup the driver before the next test.
driver.quit()