from selenium import webdriver
import string
import zipfile
proxyHost = "http-dyn.abuyun.com"
proxyPort = "9020"
proxyUsername = "H5IM8T3H288W241D"
proxyPassword = "5C4448B395A6FF16"
def create_proxy_auth_extension (proxy_host, proxy_port,
) proxy_username, proxy_password,
scheme="http", plugin_path=None):
if plugin_path is None:
plugin_path = r"./{}_{}@http-dyn.abuyun.com_9020.zip".format(proxy_username, proxy_password)
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Abuyun 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 init_driver ():
proxy_auth_plugin_path = create_proxy_auth_extension(proxy_host=proxyHost, proxy_port=proxyPort,
proxy_username=proxyUsername, proxy_password=proxyPassword,
scheme="http", plugin_path=None)
options = webdriver.ChromeOptions()
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--hide-scrollbars")
options.add_argument("--headless")
options.add_argument("--test-type")
options.add_argument("--no-first-run")
options.add_argument("--ignore-certificate-errors")
options.add_argument("--no-default-browser-check")
options.add_extension(proxy_auth_plugin_path)
return webdriver.Chrome(options=options)
if name ="_ _ main__":
driver = init_driver()
url = "http://www.baidu.com"
driver.get(url)
print(driver.page_source)
< H1 > the exception is as follows: < / H1 >
selenium.common.exceptions.WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://kidcehangkjhnmdccclekheeighbkegc/_generated_background_page.html
from unknown error: page could not be found: chrome-extension://kidcehangkjhnmdccclekheeighbkegc/_generated_background_page.html
(Driver info: chromedriver=2.41.578706 (5f725d1b4f0a4acbf5259df887244095596231db), platform=Mac OS X 10.11.6 x86x64)