[英]NoSuchElementException with headless chrome and selenium
I am trying to use headless chrome for our selenium tests and have made the below changes:我正在尝试使用无头 chrome 进行硒测试,并进行了以下更改:
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.当我使用 --headless 运行测试时。
Chrome Version: 62.0.3202.89 chromeDriver: 2.33.506120 Selenium version: 2.53.0 Windows 7 Chrome 版本:62.0.3202.89 chromeDriver:2.33.506120 Selenium 版本:2.53.0 Windows 7
我有同样的问题,我的错误是因为我正在制作 driver.get("localhost:...") 而不是 driver.get(" http://localhost :...")
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.我在无头运行时也遇到了类似的问题,我的项目在无头运行时继续触发 NoSuchElementException 并且默认浏览器检查正在妨碍,请尝试添加这些参数。 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");
当您看到NoSuchElementException 时,您可以考虑将xpath
与服务员一起使用,以使元素可见,如下所示:
//tr/td/div[@class='x-grid-cell-inner' and contains(., 'Global Test Merchant')]
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");
In my particular case I only had to use these 3 arguments to solve the same issue:在我的特殊情况下,我只需要使用这 3 个参数来解决相同的问题:
options.addArguments("--headless");
options.addArguments("--start-maximized");
options.addArguments("--window-size=1920,1080");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.