【问题标题】:How to set up Selenium and ChromeDriver to run in headless mode如何设置 Selenium 和 ChromeDriver 以在无头模式下运行
【发布时间】:2018-02-08 16:41:46
【问题描述】:

我正在尝试使用 xUnit、Selenium 和 Chrome Canary(无头模式)运行浏览器测试,但我不断收到此错误:

OpenQA.Selenium.WebDriverException
The HTTP request to the remote WebDriver server for URL 
http://localhost:58692/session timed out after 60 seconds.

这是我的代码:

var chromeOptions = new ChromeOptions
    {
        BinaryLocation = @"C:\Users\<USERNAME>\AppData\Local\Google\Chrome SxS\Application\chrome.exe",
        DebuggerAddress = "127.0.0.1:9222"
    };

chromeOptions.AddArguments("no-sandbox", "headless", "disable-gpu");
_driver = new ChromeDriver(chromeOptions) {Url = Url};

我对 C# 很陌生,所以我不确定我是否做错了什么,或者我只是错过了一个设置。谷歌搜索上述错误告诉我,我需要设置调试器地址并使用 no-sandbox 标志,但似乎都没有帮助。

使用这些版本:

selenium 3.4
chromedriver 2.29
xunit 2.2

【问题讨论】:

  • 非常有帮助,谢谢。我必须使用 Chrome 60 测试版才能让它无头运行。

标签: c# selenium selenium-chromedriver xunit


【解决方案1】:

取出调试器地址使其工作。

var chromeOptions = new ChromeOptions
{
    BinaryLocation = @"C:\Users\<USERNAME>\AppData\Local\Google\Chrome SxS\Application\chrome.exe",
};

【讨论】:

    【解决方案2】:

    以防其他人在谷歌搜索时到达这里......

    对我有用的是从此链接下载最新的 chrome-driver: https://sites.google.com/a/chromium.org/chromedriver/downloads

    编码愉快:)

    【讨论】:

      【解决方案3】:
                  case "chrome9515headless":
                      ChromeOptions chromeOptions = new ChromeOptions();
                      chromeOptions.AddArgument("--headless");
                      chromeOptions.AddArgument("--disable-gpu");
                      chromeOptions.AddArgument("--disable-infobars");
                      chromeOptions.AddArgument("--disable-extensions");
                      chromeOptions.AddArgument("--window-size=1200,900");
                      chromeOptions.AddArgument("--disable-browser-side-navigation");
      
                      webDriver = new RemoteWebDriver(new Uri("http://127.0.0.1:9515"),
                          chromeOptions.ToCapabilities());
                      break;
                  case "chrome9515canary":
                      ChromeOptions chromeOptionsCanary = new ChromeOptions();
                      chromeOptionsCanary.BinaryLocation= @"C:\Users\********\AppData\Local\Google\Chrome SxS\Application\chrome.exe";
                      //chromeOptionsCanary.AddArgument("--headless");
                      //chromeOptions.AddArgument("--disable-gpu");
                      chromeOptionsCanary.AddArgument("--disable-infobars");
                      chromeOptionsCanary.AddArgument("--disable-extensions");
                      chromeOptionsCanary.AddArgument("--window-size=1200,900");
                      chromeOptionsCanary.AddArgument("--disable-browser-side-navigation");
      
                      webDriver = new RemoteWebDriver(new Uri("http://127.0.0.1:9515"),
                          chromeOptionsCanary.ToCapabilities());
                      break;
      

      【讨论】:

        猜你喜欢
        • 2019-10-31
        • 2022-06-13
        • 2018-06-11
        • 2014-12-31
        • 2019-09-07
        • 2019-09-13
        • 2017-12-21
        • 2023-01-22
        • 1970-01-01
        相关资源
        最近更新 更多