硒测试对无头铬的测试失败 [英] selenium tests fail against headless chrome
问题描述
我正在尝试使硒测试自动化系统针对无头铬运行,以便将其移至TeamCity.我还没有运气.当我运行它时,Chrome确实可以无脑地运行(没有浏览器弹出),但是我得到了NoSuchElementException
.当无头运行时,自动化将按预期工作.拍摄的快照仅显示一个白色矩形.
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.
我已经对该问题进行了广泛的研究,但是找不到适合我的解决方案.看来该问题是在 https://bugs.chromium.org中报告的. /p/chromedriver/issues/detail?id = 476 ,但已标记为固定.我认为问题可能出在错误的chromedriver,或者错误的chromedriver/Selenium组合,但是我尝试了各种各样的组合,但是没有爱.
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.
我正在使用:
- selenium-java 3.6.0
- chromedriver 2.33.506120
- Windows 7 Enterprise Service Pack1,64位
我的代码是:
...
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"));
...
输出为:
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
这使我无法将我的测试自动化作为CI的一部分,因此,我们将不胜感激.
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.
推荐答案
这对我有用:
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);
从
这篇关于硒测试对无头铬的测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!