-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples_selenium.py
More file actions
29 lines (21 loc) · 832 Bytes
/
examples_selenium.py
File metadata and controls
29 lines (21 loc) · 832 Bytes
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
import os
from proxy_relay import create_proxy
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
UPSTREAM_PROXY = os.getenv("UPSTREAM_PROXY", "socks5://user:pass@proxy.com:1080")
TEST_URL = "https://api.ipify.org/"
def main() -> None:
"""Use Selenium (Chrome) with a local proxy created by proxy_relay."""
# Convert upstream proxy to a local HTTP proxy that Chrome understands
local_url = create_proxy(UPSTREAM_PROXY, local_type="http")
options = Options()
# Chrome accepts scheme://host:port, e.g. http://127.0.0.1:12345
options.add_argument(f"--proxy-server={local_url}")
driver = webdriver.Chrome(options=options)
try:
driver.get(TEST_URL)
print(driver.page_source)
finally:
driver.quit()
if __name__ == "__main__":
main()