Selenium in Java findet kein Element, wenn Headless Chrome verwendet wird
Aug 28 2020
Ich möchte einige Elemente von einer JavaScript-basierten Website finden. Ich benutze Java und Selen. Alles funktioniert gut, aber wenn ich Headless Chrome verwenden möchte, kann Selenium die Elemente nicht finden.
Ich habe diese Optionen zu meinem ChromeDriver hinzugefügt:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
Konsole:
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
Antworten
1 DebanjanB Aug 28 2020 at 14:01
Während Sie den Headless- Modus verwenden, müssen Sie:
Maximieren Sie das Browserfenster
ChromeOptions options = new ChromeOptions(); options.addArguments("--window-size=1400,600");Anstatt WebDriverWait wie folgt zu
presenceOfElementLocated()induzieren :visibilityOfElementLocated()WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-id='current-price']")));