问题
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:
- selenium-java 3.6.0
- chromedriver 2.33.506120
- Windows 7 Enterprise Service Pack1, 64-bit
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.
回答1:
I had the same issue, the local server was using selfsigned certificate, here is the combination that worked for me:
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
options.addArguments("--allow-insecure-localhost");
回答2:
This is what worked for me:
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("--headless");
chromeOptions.AddArguments("--disable-gpu");
chromeOptions.AddArguments("--window-size=1280,800");
chromeOptions.AddArguments("--allow-insecure-localhost");
//specifically this line here :)
chromeOptions.AddAdditionalCapability("acceptInsecureCerts", true, true);
Found from https://bugs.chromium.org/p/chromium/issues/detail?id=721739
回答3:
Try this:
final ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--window-size=1280,800");
WebDriver driver = new ChromeDriver(options);
回答4:
Your chromedriver/selenium combination looks perfect. Seems to me a puely synchronization issue. We need to induce some wait to syck up as follows:
driver.get(url);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement usernameTextfield = wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.cssSelector(".input.username"))));
usernameTextfield.sendKeys("user_name");
回答5:
For anyone who may stumble here with this error, but is utilizing python.
I was having issues with a script working only when headless was not utilized.
Initially, I had my options looking like this:
options = Options()
options.headless = True
After finding this thread, I altered my options to the following:
options = Options()
options.add_argument("--headless");
options.add_argument("--window-size=1440, 900")
It appears that headless windows are formatted differently when selenium navigates the page. Go figure. This solved all of my issues.
回答6:
I had the exact same problem.
You need to add to your options your computer's user agent,
To search your user agent just type in google: ״my user agent
״
Then add it to the options: options.add_argument("your-user-agent")
来源:https://stackoverflow.com/questions/47061662/selenium-tests-fail-against-headless-chrome