如何使用远程驱动程序在硒中身份验证代理



我正在使用Selenium(Chrome驱动程序(进行测试,我需要向其添加带有身份验证的代理。现在我使用本地chromedriver文件和此指令来身份验证代理

但是当我尝试连接到远程驱动程序(使用独立的铬 docker 时(

我试过什么

  1. 我尝试对远程驱动程序使用与本地驱动程序相同的指令

  2. 创建功能:

这是用于创建扩展的代码

    def create_extension():
        """
        Create extension for auth proxy
        """
        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_login', 'proxy_pass')
        pluginfile = '/tmp/proxy_auth_plugin.zip'
        logger.info("Saving zip plugin file to {}".format(pluginfile))
        with zipfile.ZipFile(pluginfile, 'w') as zp:
            zp.writestr("manifest.json", manifest_json)
            zp.writestr("background.js", background_js)
        return pluginfile
    def get_chrome_options():
        """
        :return: ChromeOptions
        """
        extension_path = self.create_extension()
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_extension(extension_path)
        chrome_options.add_argument("--ignore-certificate-errors")
        chrome_options.add_argument("--disable-notifications")
        chrome_options.add_argument("--disable-gpu")
        chrome_options.add_argument("--disable-dev-shm-usage")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--headless")
        capabilities = chrome_options.to_capabilities()
        return chrome_options, capabilities

以及我如何运行驱动程序

chrome_options, capabilities = get_chrome_options()
driver = webdriver.Remote(command_executor='localhost:4444/wd/hub', desired_capabilities=capabilities)

我有错误:

selenium.common.exceptions.SessionNotCreatedException: Message: Unable to create session from {
  "desiredCapabilities": {
    "browserName": "chrome",
    "goog:chromeOptions": {
      "args": [
        "--ignore-certificate-errors",
        "--disable-notifications",
        "--disable-gpu",
        "--disable-dev-shm-usage",
        "--no-sandbox",
        "--headless"
      ],
      "extensions": "u002ftmpu002fproxy_auth_plugin.zip"
    },
    "version": "",
    "platform": "ANY"
  },
  "capabilities": {
    "firstMatch": [
      {
        "browserName": "chrome",
        "goog:chromeOptions": {
          "args": [
            "--ignore-certificate-errors",
            "--disable-notifications",
            "--disable-gpu",
            "--disable-dev-shm-usage",
            "--no-sandbox",
            "--headless"
          ],
          "extensions": "u002ftmpu002fproxy_auth_plugin.zip"
        }
      },
      {
        "browserName": "chrome",
        "platformName": "any",
        "goog:chromeOptions": {
          "args": [
            "--ignore-certificate-errors",
            "--disable-notifications",
            "--disable-gpu",
            "--disable-dev-shm-usage",
            "--no-sandbox",
            "--headless"
          ],
          "extensions": "u002ftmpu002fproxy_auth_plugin.zip"
        }
      }
    ]
  }
}
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: '6b342da0476d', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.125-linuxkit', java.version: '1.8.0_191'
Driver info: driver.version: unknown
Stacktrace:
    at org.openqa.selenium.remote.server.NewSessionPipeline.lambda$null$4 (NewSessionPipeline.java:76)
    at java.util.Optional.orElseThrow (Optional.java:290)
    at org.openqa.selenium.remote.server.NewSessionPipeline.lambda$createNewSession$5 (NewSessionPipeline.java:75)
    at java.util.Optional.orElseGet (Optional.java:267)
    at org.openqa.selenium.remote.server.NewSessionPipeline.createNewSession (NewSessionPipeline.java:73)
    at org.openqa.selenium.remote.server.commandhandler.BeginSession.execute (BeginSession.java:65)
    at org.openqa.selenium.remote.server.WebDriverServlet.lambda$handle$0 (WebDriverServlet.java:235)
    at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:511)
    at java.util.concurrent.FutureTask.run (FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
    at java.lang.Thread.run (Thread.java:748)

具有能力

proxy = {'address': 'host:port',
                         'username': 'login',
                         'password': 'pass'}
capabilities = dict(DesiredCapabilities.CHROME)
capabilities['proxy'] = {'proxyType': 'MANUAL',
                         'httpProxy': proxy['address'],
                         'ftpProxy': proxy['address'],
                         'sslProxy': proxy['address'],
                         'noProxy': '',
                         'class': "org.openqa.selenium.Proxy",
                         'autodetect': False}
capabilities['proxy']['socksUsername'] = proxy['username']
capabilities['proxy']['socksPassword'] = proxy['password']
driver = webdriver.Remote(
                    command_executor='localhost:4444/wd/hub',
                    desired_capabilities=capabilities
                )

具有此功能的远程浏览器没有答案

我试图关闭当前goog:chromeOptions但它没有影响

我希望为远程驱动程序授权代理

我发现只有一种方法可以做到这一点:

使用鱿鱼作为反向代理

  1. 鱿鱼中的身份验证代理
  2. 使用鱿鱼作为反向代理
  3. 如果我需要更改代理,我可以更改 squid.conf 并重新启动鱿鱼进程

最新更新