简体   繁体   English

无头和代理身份验证 Selenium Python

[英]Headless and Proxy authentication Selenium Python

I'm looking for a way to make proxies work with authentication and headless.我正在寻找一种使代理与身份验证和无头工作的方法。 I tried this:我试过这个:

import os
import zipfile

PROXY_HOST = 'ooooo.com'  # rotating proxy or host
PROXY_PORT = 12345 # port
PROXY_USER = 'User_Proxy' # username
PROXY_PASS = 'Password:proxy' # password


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 = """
var config = {
        mode: "fixed_servers",
        rules: {
        singleProxy: {
            scheme: "http",
            host: "%s",
            port: parseInt(%s)
        },
        bypassList: ["localhost"]
        }
    };

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
    return {
        authCredentials: {
            username: "%s",
            password: "%s"
        }
    };
}

chrome.webRequest.onAuthRequired.addListener(
            callbackFn,
            {urls: ["<all_urls>"]},
            ['blocking']
);
""" % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)


def get_chromedriver(use_proxy=False, user_agent=None):
    path = os.path.dirname(os.path.abspath(__file__))
    chrome_options = webdriver.ChromeOptions()
    #chrome_options = webdriver.Chrome
    if use_proxy:
        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_argument("--window-size=1920,1080")
            chrome_options.add_argument('--headless')
        chrome_options.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    driver = webdriver.Chrome(os.path.join(path, 'chromedriver'), 
             chrome_options=chrome_options)
    driver.get("https://ita.privateinternetaccess.com/pages/whats-my-ip/")

    print(driver.page_source)

driver = get_chromedriver(use_proxy=True)

But it gives an error:但它给出了一个错误:

selenium.common.exceptions.WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://fboilabmbpogaekkclhheepnkjcfifdn/_generated_background_page.html from unknown error: page could not be found: chrome-extension://fboilabmbpogaekkclhheepnkjcfifdn/_generated_background_page.html selenium.common.exceptions.WebDriverException:消息:未知错误:未能等待扩展背景页面加载:chrome-extension://fboilabmbpogaekkclhheepnkjcfifdn/_generated_background_page.html无法从未知页面找到chrome-extension: fboilabmbpogaekkclhheepnkjcfifdn/_generated_background_page.html

You are basically trying to add a browser extension whilst in headless mode.您基本上是在尝试在无头模式下添加浏览器扩展。 Which sadly is not supported.遗憾的是不支持。 If you found a solution to this please post the answer here.如果您找到了解决方案,请在此处发布答案。 I am sure a lot of people are having the same issue (including me)我相信很多人都有同样的问题(包括我)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM