How to set up Selenium and ChromeDriver to run in headless mode

让人想犯罪 __ 提交于 2019-12-23 18:10:20

问题


I am trying to run a browser test using xUnit, Selenium, and Chrome Canary (headless mode) but I keep getting this error:

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

Here's my code:

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};

I'm quite new to C# so I'm not sure if I'm doing something blatantly wrong, or if I'm just missing a setting. Googling the above error told me that I need to set the debugger address and use the no-sandbox flag, but neither seem to be helping.

Using these versions:

selenium 3.4
chromedriver 2.29
xunit 2.2

回答1:


Taking out the debugger address made it work.

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



回答2:


In case that others will get here while googling...

What worked for me was to download the latest chrome-driver from this link: https://sites.google.com/a/chromium.org/chromedriver/downloads

Happy coding :)




回答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;


来源:https://stackoverflow.com/questions/43668821/how-to-set-up-selenium-and-chromedriver-to-run-in-headless-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!