I am trying to get my selenium test automation to run against headless chrome so that I can move it to TeamCity. I have not had any luck. When I run it, Chrome does appear to run headlessly (no browser pops up), but I get a NoSuchElementException
. The automation works as expected when run non-headlessly. A snapshot taken just shows a white rectangle.
I have researched this issue extensively, but I have not been able to find a solution that works for me. It appears that the issue was reported in https://bugs.chromium.org/p/chromedriver/issues/detail?id=476, but it's marked fixed. I think the problem might be the wrong chromedriver, or maybe the wrong chromedriver/selenium combination, but I've tried all sorts of combinations and no love.
I am using:
My code is:
...
ChromeOptions headlessOptions = new ChromeOptions();
headlessOptions.addArguments("--start-maximized");
headlessOptions.addArguments("--headless");
driver = new ChromeDriver(headlessOptions);
driver.get(url);
WebElement usernameTextfield = driver.findElement(By.cssSelector(".input.username"));
...
And the output is:
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 41402
Only local connections are allowed.
Nov 01, 2017 10:22:51 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".input.username"}
(Session info: headless chrome=62.0.3202.75)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
This is preventing me from being able to include my test automation as part of our CI, so any help would be very much appreciated.
Try this:
final ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--window-size=1280,800");
WebDriver driver = new ChromeDriver(options);
Adding the user-agent did the job for me:
--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With