首页 > 解决方案 > 使用 Chrome 的 Selenium 代理身份验证不允许 https 请求

问题描述

我正在尝试构建一个运行 selenium webdriver 的 python 脚本,进行代理身份验证(使用用户和密码)并通过 https 调用加载网页。我搜索了互联网,找到了成功设置 http 代理连接的最新方法。但是,当尝试通过 https 加载网页时,而不是像我看到的示例那样使用 http(这是我需要的),Chrome 驱动程序会显示“页面永久移动或无法访问”。

下面是我使用的代码。

任何帮助都将不胜感激。肿瘤坏死因子。

硒脚本代码:

PROXY_HOST = 'http://www.myproxy.com'  
PROXY_PORT = 80
PROXY_USER = 'user'
PROXY_PASS = '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()
    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_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--start-maximized')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('window-size=1200,1100')
    driver = webdriver.Chrome(
        os.path.join(path, 'chromedriver'),
        chrome_options=chrome_options)
    return driver

driver = get_chromedriver(use_proxy=True)

driver.get("https://www.msysecurepage.com")

标签: seleniumselenium-webdriver

解决方案


推荐阅读