NoSuchElementException with headless chrome and selenium
Asked Answered
D

6

10

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

Digitalize answered 27/3, 2018 at 19:0 Comment(2)
so how the problem could be solved?Maishamaisie
Same issue for me with python. It's OK in normal mode mode but throws in headless.Bondswoman
C
2

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

Carlin answered 23/6, 2019 at 12:12 Comment(0)
P
1

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");
Prowler answered 29/3, 2018 at 5:1 Comment(1)
I tried the above solution but i still get the same exception.Digitalize
A
0

As you are seeing NoSuchElementException you can consider using the xpath along with a waiter for the element to be visible as follows :

//tr/td/div[@class='x-grid-cell-inner' and contains(., 'Global Test Merchant')]
Ambsace answered 28/3, 2018 at 2:42 Comment(2)
I have tried with implicit waits but it doesn't work. This problem happens only in headless mode and not otherwise.Digitalize
Can you update the question with the relevant HTML, your code trial and the error stack trace for further analysis?Ambsace
E
0

I had a similar issue.

I tried following options and it worked for me.

    options.addArguments("--window-size=1920,1080");
    options.addArguments("--disable-extensions");
    options.addArguments("--proxy-server='direct://'");
    options.addArguments("--proxy-bypass-list=*");
    options.addArguments("--start-maximized");
    options.addArguments("--headless");
    options.addArguments("--disable-gpu");
    options.addArguments("--disable-dev-shm-usage");
    options.addArguments("--no-sandbox");
    options.addArguments("--ignore-certificate-errors");
Exchequer answered 31/1, 2020 at 7:0 Comment(0)
C
0

In my particular case I only had to use these 3 arguments to solve the same issue:

options.addArguments("--headless");
options.addArguments("--start-maximized");
options.addArguments("--window-size=1920,1080");
Crackerbarrel answered 13/10, 2020 at 17:30 Comment(0)
F
0

In my case adding this line (after line where exception shows) helped.

driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
Footwork answered 22/6, 2023 at 12:6 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Hamish

© 2022 - 2024 — McMap. All rights reserved.