Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSuchElementException with headless chrome and selenium

I am trying to use headless chrome for our selenium tests and have made the below changes:

DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("window-size=1800x1080");
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);

My test logs into an internal page and then waits for the element to be visible:

selenium.waitForElementVisible("xpath=//tr/td/div[@class[contains(., 'x-grid-cell-inner')] and text()='Global Test Merchant 14']");

This all works well when i do not have the headless option but i get :

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:  {"method":"xpath","selector":"//tr/td/div[@class[contains(., 'x-grid-cell-inner')] and text()='Global Test Merchant 14']"} 

when i run the test with --headless.

Chrome Version: 62.0.3202.89 chromeDriver: 2.33.506120 Selenium version: 2.53.0 Windows 7

like image 875
mishra Avatar asked Mar 27 '18 19:03

mishra


People also ask

Does Selenium use headless Chrome?

Selenium WebDriver provides a class called "ChromeOptions", which can specify certain configurations to change the default behavior of Chrome. One of those configurations is the "headless" mode, which launches the Chrome in headless mode while running the test cases.

How do I run Chrome headless mode in Selenium?

You can run Google Chrome in headless mode simply by setting the headless property of the chromeOptions object to True. Or, you can use the add_argument() method of the chromeOptions object to add the –headless command-line argument to run Google Chrome in headless mode using the Selenium Chrome web driver.

Does Selenium support headless browser?

Headless testing is simply running your Selenium tests using a headless browser. It operates as your typical browser would, but without a user interface, making it excellent for automated testing.

How do I run Chrome in headless mode?

Which command starts the google chrome web browser in headless mode? As we have already seen, you just have to add the flag –headless when you launch the browser to be in headless mode. With CLI (Command Line Interface), just write: chrome \<br> – headless \ # Runs Chrome in headless mode.


2 Answers

I had the same issue, my mistake was because i was making driver.get("localhost:...") instead of driver.get("http://localhost:...")

like image 129
Lucas Barros Avatar answered Sep 21 '22 08:09

Lucas Barros


I had a similar issue when I ran headless as well, my project while running headless continue to trigger the NoSuchElementException and the default-browser-check was getting in the way, try adding these arguments. Just a thought

chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--test-type");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--no-first-run");
chromeOptions.addArguments("--no-default-browser-check");
chromeOptions.addArguments("--ignore-certificate-errors");
chromeOptions.addArguments("--start-maximized");
like image 24
Bobby Mor Avatar answered Sep 17 '22 08:09

Bobby Mor