Selenium在Java中使用无头Chrome时无法找到元素。

huangapple go评论130阅读模式
英文:

Selenium in Java is not finding element when using headless Chrome

问题

  1. 我想从一个基于 JavaScript 的网站中提取一些元素。我使用 Java Selenium。一切都正常工作,但是当我想使用无头 Chrome 时,Selenium 无法找到这些元素。
  2. 我将以下选项添加到了我的 ChromeDriver

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");

  1. 控制台输出:
  2. ```plaintext
  3. ChromeDriver 已成功启动。
  4. 2020-08-27 22:41:46.625 INFO 20344 --- [ null to remote] o.o.selenium.remote.ProtocolHandshake : 检测到的协议:W3C
  5. Exception in thread "Thread-8" Exception in thread "Thread-10" org.openqa.selenium.TimeoutException: 期望条件未满足:等待元素出现,定位方式:By.cssSelector: [data-id='current-price'](尝试了 5 秒,每次间隔 500 毫秒)
  6. at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
  7. at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
  8. at magharmi.asos.pricechecker.controller.PriceController$1.run(PriceController.java:52)
  9. Caused by: org.openqa.selenium.NoSuchElementException: 没有这样的元素:无法定位元素:{"method":"css selector","selector":"[data-id='current-price']"}
  10. (Session info: headless chrome=85.0.4183.83)
  11. 有关此错误的文档,请访问:https://www.seleniumhq.org/exceptions/no_such_element.html
  12. 构建信息:版本:'3.141.59',修订版本:'e82be7d358',时间:'2018-11-14T08:17:03'
  13. 系统信息:主机:'DESKTOP-MMDJR4G',IP:'192.168.178.45',操作系统名称:'Windows 10',操作系统架构:'amd64',操作系统版本:'10.0',Java 版本:'13.0.1'
  14. 驱动程序信息: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:

  1. ChromeOptions chromeOptions = new ChromeOptions();
  2. chromeOptions.addArguments("--headless");

Console:

  1. ChromeDriver was started successfully.
  2. 2020-08-27 22:41:46.625 INFO 20344 --- [ null to remote] o.o.selenium.remote.ProtocolHandshake : Detected dialect: W3C
  3. 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)
  4. at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
  5. at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
  6. at magharmi.asos.pricechecker.controller.PriceController$1.run(PriceController.java:52)
  7. Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[data-id='current-price']"}
  8. (Session info: headless chrome=85.0.4183.83)
  9. For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
  10. Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
  11. 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'
  12. Driver info: org.openqa.selenium.chrome.ChromeDriver

答案1

得分: 1

在使用 [tag:headless] 模式时,您需要:

  • 最大化浏览窗口

    1. ChromeOptions options = new ChromeOptions();
    2. options.addArguments("--window-size=1400,600");
  • 在使用 presenceOfElementLocated() 时,改为使用 WebDriverWait 来等待 visibilityOfElementLocated(),方法如下:

    1. WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-id='current-price']")));
英文:

While using [tag:headless] mode you need to:

  • Maximize the browsing window

    1. ChromeOptions options = new ChromeOptions();
    2. options.addArguments("--window-size=1400,600");
  • Instead of presenceOfElementLocated() induce WebDriverWait for visibilityOfElementLocated() as follows:

    1. WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-id='current-price']")));

huangapple
  • 本文由 发表于 2020年8月28日 04:44:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63623917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定