-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselenium_python.py
More file actions
44 lines (31 loc) · 1.22 KB
/
Copy pathselenium_python.py
File metadata and controls
44 lines (31 loc) · 1.22 KB
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
"""Selenium + ChromeDriver through a local proxy hop.
Start the hop first, in another terminal:
BOTPROXY_USERNAME=pxu1000-0 BOTPROXY_PASSWORD=your-password \\
python3 python/proxy_hop.py
then run this:
pip install selenium
python3 examples/selenium_python.py
Chrome never sees a credential, so there is no auth prompt to dismiss, no
extension to build, and nothing that a Chrome release can take away.
"""
from selenium import webdriver
HOP = "127.0.0.1:8888"
def make_driver(headless=True):
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=http://%s" % HOP)
# Chrome resolves the target itself when it talks to a proxy, but it will
# still bypass the proxy for localhost unless told otherwise. Harmless
# here, and it keeps a local test server reachable.
options.add_argument("--proxy-bypass-list=<-loopback>")
if headless:
options.add_argument("--headless=new")
return webdriver.Chrome(options=options)
def main():
driver = make_driver()
try:
driver.get("https://httpbin.org/ip")
print("exit IP:", driver.find_element("tag name", "pre").text.strip())
finally:
driver.quit()
if __name__ == "__main__":
main()