【问题标题】:Selenium headless chrome with proxy authentication具有代理身份验证的 Selenium 无头 chrome
【发布时间】:2022-06-10 21:35:34
【问题描述】:

我正在使用 Selenium (4.1.0) 和 C#(.Net 5.0/Console 应用程序)自动化 Headless Chrome WebDriver (100.0.48.96.60)。我想添加代理身份验证,但似乎没有什么对我有用。

到目前为止我已经尝试过:

  1. Selenium 的 BiDi API:https://www.selenium.dev/documentation/webdriver/bidirectional/bidi_api/#register-basic-auth 它会随机导致 2 个异常(可能在打开 url 时):

System.InvalidOperationException: '未收到命令响应:Fetch.getResponseBody'

OpenQA.Selenium.DevTools.CommandResponseException: 'Fetch.continueRequest: Invalid InterceptionId.'

  1. 代理身份验证扩展:https://github.com/RDavydenko/OpenQA.Selenium.Chrome.ChromeDriverExtensions。它不适用于无头 chrome。
  2. 汽车信息技术。我认为也不适用于无头 chrome。
  3. 像这样设置代理:

"用户名:密码@IP:PORT"

我能找到的所有答案要么不适用于 headless,要么不再适用。因此,请随意留下您对该主题的任何想法。

【问题讨论】:

    标签: c# selenium proxy selenium-chromedriver headless


    【解决方案1】:

    这个响应是用 Java 编写的,但你可以将它调整为具有两个库的 C#, 它使用带有硒的 BI-DI 的动作,它在无头 (https://www.browserstack.com/docs/automate/selenium/event-driven-testing#register-basic-auth) 中为我工作。

    您有 4 个变量要填写 PROXY_HOST、PROXY_PORT、PROXY_USER、PROXY_PASS:

        ChromeOptions options = new ChromeOptions();
        options.setHeadless(true);
        options.setProxy(new Proxy().setHttpProxy(PROXY_HOST + ":" + PROXY_PORT));
        driver = new ChromeDriver(options);
        DevTools devTools = ((HasDevTools) driver).getDevTools();
        devTools.createSession();
        driver = new Augmenter().
                addDriverAugmentation("chrome", HasAuthentication.class, (caps, exec) -> (whenThisMatches, useTheseCredentials) -> devTools.getDomains().network().addAuthHandler(whenThisMatches, useTheseCredentials)).augment(driver);
        ((HasAuthentication) driver).register(UsernameAndPassword.of(PROXY_USER, PROXY_PASS));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2023-03-26
      • 2020-06-09
      • 2019-01-05
      • 2021-10-07
      • 2022-08-19
      相关资源
      最近更新 更多