5
votes

After starting chromedriver.exe in headless mode following this advice and using just these arguments

 options.AddArgument("headless");
 options.AddArgument("window-size=1280,960");

The chromedriver opens invisibly. But Selenium's FindElement() command is not finding anything on the headless Chrome page. Instead it throws this exception:

An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code

Additional information: no such element: Unable to locate element:

Q1: Has anyone had success running Selenium commands in Chrome's headless mode?

Q2: Have you been able to use FindElement with a chromedriver running in headless mode? If yes, how did you do it?


After reading more, perhaps something along these lines may be necessary? Add this to the Chrome startup options and then maybe connect chromedriver to it?
"remote-debugging-port=9222"
But with that option the IWebDriver and chromedriver does not open.


Background info: to answer, why would you want to do this? The primary reason was for tests run as a part of CI. These are tests that run on a VM and may not support 1080p monitors. If we ran it in headless mode and set the resolution that way we could.

1
SpecFlow has no FindElements. It has nothing to do with browser automation. This issue is about Selenium.Andreas Willich
I use Selenium + Chrome headless all the time. Don't know how that helps you, as I do not understand what your question is. Perhaps have a read through How to Ask.SiKing
It seems like this issue comes if there's a proxy involved (like proxymob or similar) while using Selenium + Chrome + SomeProxy (for diverting calls to end points while running your non-Unit tests like IT/FT/Selenium browser based tests etc) + running tests in HEADLESS mode. I would say try using a different proxy server (Traefik etc).AKS
It may be related to this bug: github.com/SeleniumHQ/selenium/issues/4477Phyxx

1 Answers

1
votes

Add below lines of code in your main class:

ChromeOptions options = new ChromeOptions(); 
options.setHeadless(true); 
options.addArguments("--window-size=1920,1080"); 
options.addArguments("--disable-gpu"); 
options.addArguments("--disable-extensions"); options.setExperimentalOption("useAutomationExtension", false); options.addArguments("--proxy-server='direct://'"); 
options.addArguments("--proxy-bypass-list=*"); 
options.addArguments("--start-maximized");
options.addArguments("--headless"); 
WebDriver driver = new ChromeDriver(options);