failed to wait for extension background page to load

发布于 2022-09-11 15:03:26 字数 3496 浏览 19 评论 0

代码如下:

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)

异常如下:

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 x86_64)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭泣的笑容 2022-09-18 15:03:26

可以通过squid进行解决

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文