gpt4 book ai didi

java - 使用 headless Chrome 时,Java 中的 Selenium 找不到元素

转载 作者:行者123 更新时间:2023-12-04 03:57:21 24 4
gpt4 key购买 nike

我想从基于 JavaScript 的网站中查找一些元素。我使用 Java 和 Selenium 。一切正常,但当我想使用 headless Chrome 时,Selenium 无法找到元素。

我将此选项添加到我的 ChromeDriver:

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

控制台:

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

最佳答案

在使用 时您需要的模式:

  • Maximize the browsing window

    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']")));

关于java - 使用 headless Chrome 时,Java 中的 Selenium 找不到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63623917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com