如何使用远程驱动程序对硒中的代理进行身份验证

时间:2019-03-27 14:48:57

标签: python selenium selenium-chromedriver

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

但是当我尝试连接到远程驱动程序时(使用standalone-chrome泊坞窗)

我尝试了什么

  1. 我尝试使用与本地驱动器相同的instruction远程驱动程序

  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": "\u002ftmp\u002fproxy_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": "\u002ftmp\u002fproxy_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": "\u002ftmp\u002fproxy_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 个答案:

答案 0 :(得分:0)

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

使用squid作为反向代理

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