from selenium.webdriver.chrome.service import Service
import time
import string
import zipfile
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def create_proxyauth_extension(proxy_host, proxy_port,proxy_username, proxy_password,scheme='http', plugin_path=None):
"""Proxy Auth Extension
args:
proxy_host (str): domain or ip address, ie proxy.domain.com
proxy_port (int): port
proxy_username (str): auth username
proxy_password (str): auth password
kwargs:
scheme (str): proxy scheme, default http
plugin_path (str): absolute path of the extension
return str -> plugin_path
"""
if plugin_path is None:
plugin_path = 'Selenium-Chrome-HTTP-Private-Proxy.zip'
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 = string.Template(
"""
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "${scheme}",
host: "${host}",
port: parseInt(${port})
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "${username}",
password: "${password}"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
"""
).substitute(
host=proxy_host,
port=proxy_port,
username=proxy_username,
password=proxy_password,
scheme=scheme,
)
with zipfile.ZipFile(plugin_path, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return plugin_path
def configure_headless_browser(proxy_config):
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
proxyauth_plugin_path = create_proxyauth_extension(
proxy_host=proxy_config[0],
proxy_port=proxy_config[1],
proxy_username=proxy_config[2],
proxy_password=proxy_config[3]
)
chrome_options.add_extension(proxyauth_plugin_path)
return webdriver.Chrome(options=chrome_options, service=Service(r'D:\test\chromedriver.exe'))
# 设置代理IP:分别填入代理ip,端口,账号,密码
proxy_config = ["10.10.30.90", "80", "username", "password"]
driver = configure_headless_browser(proxy_config)
driver.get('http://httpbin.org/ip')
Python Selenium 本机开启代理,谷歌浏览器关闭代理登录弹窗配置
最新推荐文章于 2024-09-17 23:15:58 发布