英文:
Selenium in Java is not finding element when using headless Chrome
问题
我想从一个基于 JavaScript 的网站中提取一些元素。我使用 Java 和 Selenium。一切都正常工作,但是当我想使用无头 Chrome 时,Selenium 无法找到这些元素。
我将以下选项添加到了我的 ChromeDriver:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
控制台输出:
```plaintext
ChromeDriver 已成功启动。
2020-08-27 22:41:46.625 INFO 20344 --- [ null to remote] o.o.selenium.remote.ProtocolHandshake : 检测到的协议:W3C
Exception in thread "Thread-8" Exception in thread "Thread-10" org.openqa.selenium.TimeoutException: 期望条件未满足:等待元素出现,定位方式:By.cssSelector: [data-id='current-price'](尝试了 5 秒,每次间隔 500 毫秒)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
at magharmi.asos.pricechecker.controller.PriceController$1.run(PriceController.java:52)
Caused by: org.openqa.selenium.NoSuchElementException: 没有这样的元素:无法定位元素:{"method":"css selector","selector":"[data-id='current-price']"}
(Session info: headless chrome=85.0.4183.83)
有关此错误的文档,请访问:https://www.seleniumhq.org/exceptions/no_such_element.html
构建信息:版本:'3.141.59',修订版本:'e82be7d358',时间:'2018-11-14T08:17:03'
系统信息:主机:'DESKTOP-MMDJR4G',IP:'192.168.178.45',操作系统名称:'Windows 10',操作系统架构:'amd64',操作系统版本:'10.0',Java 版本:'13.0.1'
驱动程序信息:org.openqa.selenium.chrome.ChromeDriver
英文:
I want to find some elements from an JavaScript based website. I use Java and Selenium. Everything works fine, but when I want to use headless Chrome, then Selenium is not able to find the elements.
I added this options to my ChromeDriver:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
Console:
ChromeDriver was started successfully.
2020-08-27 22:41:46.625 INFO 20344 --- [ null to remote] o.o.selenium.remote.ProtocolHandshake : Detected dialect: W3C
Exception in thread "Thread-8" Exception in thread "Thread-10" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.cssSelector: [data-id='current-price'] (tried for 5 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
at magharmi.asos.pricechecker.controller.PriceController$1.run(PriceController.java:52)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[data-id='current-price']"}
(Session info: headless chrome=85.0.4183.83)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-MMDJR4G', ip: '192.168.178.45', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '13.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
答案1
得分: 1
在使用 [tag:headless] 模式时,您需要:
-
ChromeOptions options = new ChromeOptions(); options.addArguments("--window-size=1400,600");
-
在使用
presenceOfElementLocated()
时,改为使用 WebDriverWait 来等待visibilityOfElementLocated()
,方法如下:WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-id='current-price']")));
英文:
While using [tag:headless] mode you need to:
-
ChromeOptions options = new ChromeOptions(); options.addArguments("--window-size=1400,600");
-
Instead of
presenceOfElementLocated()
induce WebDriverWait forvisibilityOfElementLocated()
as follows:WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-id='current-price']")));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论