-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_proxy_selenium.py
More file actions
81 lines (72 loc) · 2.01 KB
/
Copy pathtest_proxy_selenium.py
File metadata and controls
81 lines (72 loc) · 2.01 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import zipfile # Import the zipfile module
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
# Set up proxy details
proxy_host = "p.webshare.io:80"
proxy_username = "ljayoggy-JP-rotate"
proxy_password = "kfk2b2al877m"
# Configure the proxy with authentication for Selenium
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version": "22.0.0"
}
"""
background_js = f"""
var config = {{
mode: "fixed_servers",
rules: {{
singleProxy: {{
scheme: "http",
host: "{proxy_host.split(':')[0]}",
port: parseInt({proxy_host.split(':')[1]})
}},
bypassList: []
}}
}};
chrome.proxy.settings.set({{value: config, scope: "regular"}}, function() {{}});
function callbackFn(details) {{
return {{
authCredentials: {{
username: "{proxy_username}",
password: "{proxy_password}"
}}
}};
}}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{{urls: ["<all_urls>"]}},
['blocking']
);
"""
# Set up Chrome options
chrome_options = Options()
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
chrome_options.add_extension(pluginfile)
# Create WebDriver instance with proxy settings
driver = webdriver.Chrome(options=chrome_options)
try:
# Navigate to an IP-checking website to see the proxy in action
driver.get("http://httpbin.org/ip")
sleep(5)
print("Page Source:\n", driver.page_source)
finally:
driver.quit()