Python selenium chromedriver(headless)使用代理(IPV6)和身份验证

Python selenium chromedirver( headerless) use proxies( IPV6 ) with Authentication

我有需要用户名和密码才能工作的 IPV6 代理, 有什么办法可以在 ChromeDriver ( Headerless ) 中使用用户名和密码来使用这些代理。

代理格式 - ip_address:portusername:password

如果没有,那么有什么方法可以使用这些代理更改我的系统 ipv6 地址,以便 ChromeDriver 默认使用系统 IP 地址。

您可以创建简单的扩展来设置代理和处理授权

manifest.json

{
    "manifest_version": 2,
    "name": "Chrome Proxy Auth",
    "version": "1.0.0",
    "permissions": [
        "proxy",
        "tabs",
        "unlimitedStorage",
        "storage",
        "<all_urls>",
        "webRequest",
        "webRequestBlocking"
    ],
    "background": {
        "scripts": ["background.js"]
    }
}

background.js编辑host, port, username, password

var config = {
  mode: "fixed_servers",
  rules: {
    singleProxy: {
      host: "XXX.XXX.XXX.XXX",
      port: parseInt(8888)
    }
  }
};

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

function callbackFunc(details) {
  return {
    authCredentials: {
      username: "............",
      password: "............"
    }
  };
}

chrome.webRequest.onAuthRequired.addListener(
  callbackFunc, {
    urls: ["<all_urls>"]
  },
  ['blocking']
);

将两个文件添加到 .zip 存档,然后在您的 python 脚本中

options = Options()
options.add_extension('/path/top/extension.zip')

driver = webdriver.Chrome(options=options)

带扩展的解决方案不适用于无头模式。 出现错误:

selenium.common.exceptions.WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://elfcpkfenboekckgnbhldanchpkcgmfd/_generated_background_page.html
from unknown error: page could not be found: chrome-extension://elfcpkfenboekckgnbhldanchpkcgmfd/_generated_background_page.html