在 Java 中使用 selenium 以无头模式启动 chrome 给出错误

Launching chrome in headless mode with selenium in Java giving error

这是我的代码:

WebDriverManager.chromedriver().setup();
System.setProperty("webdriver.chrome.driver", "src/main/extras/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
driver = new ChromeDriver();
driver.get(url);

最后一行,获取 URL,给出了这个错误:

Exception in thread "Thread-3" java.lang.NullPointerException

在尝试以无头模式启动浏览器之前,我没有收到此错误。

这是我的完整错误信息:

"D:\Program Files\Amazon Corretto\jdk11.0.11_9\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.2\lib\idea_rt.jar=59273:C:\Program Files\JetBrains\IntelliJ IDEA 2021.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\jackg\.m2\repository\org\openjfx\javafx-controls.0.2\javafx-controls-11.0.2.jar;C:\Users\jackg\.m2\repository\org\openjfx\javafx-graphics.0.2\javafx-graphics-11.0.2.jar;C:\Users\jackg\.m2\repository\org\openjfx\javafx-fxml.0.2\javafx-fxml-11.0.2.jar;C:\Users\jackg\.m2\repository\org\openjfx\javafx-web.0.2\javafx-web-11.0.2.jar;C:\Users\jackg\.m2\repository\org\openjfx\javafx-media.0.2\javafx-media-11.0.2.jar;C:\Users\jackg\.m2\repository\org\openjfx\javafx-base.0.1\javafx-base-11.0.1.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-java.1.1\selenium-java-4.1.1.jar;C:\Users\jackg\.m2\repository\com\google\auto\service\auto-service-annotations.0.1\auto-service-annotations-1.0.1.jar;C:\Users\jackg\.m2\repository\com\google\auto\service\auto-service.0.1\auto-service-1.0.1.jar;C:\Users\jackg\.m2\repository\com\google\auto\auto-common.2\auto-common-1.2.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-devtools-v85.1.1\selenium-devtools-v85-4.1.1.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-devtools-v94.1.1\selenium-devtools-v94-4.1.1.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-devtools-v95.1.1\selenium-devtools-v95-4.1.1.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-devtools-v96.1.1\selenium-devtools-v96-4.1.1.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-edge-driver.1.1\selenium-edge-driver-4.1.1.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-firefox-driver.1.1\selenium-firefox-driver-4.1.1.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-firefox-xpi-driver.1.1\selenium-firefox-xpi-driver-4.1.1.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-ie-driver.1.1\selenium-ie-driver-4.1.1.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-opera-driver.1.1\selenium-opera-driver-4.1.1.jar;C:\Users\jackg\.m2\repository\io\netty\netty-codec.1.70.Final\netty-codec-4.1.70.Final.jar;C:\Users\jackg\.m2\repository\io\netty\netty-handler.1.70.Final\netty-handler-4.1.70.Final.jar;C:\Users\jackg\.m2\repository\io\netty\netty-resolver.1.70.Final\netty-resolver-4.1.70.Final.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-exporter-logging.9.1\opentelemetry-exporter-logging-1.9.1.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-sdk-metrics.9.1-alpha\opentelemetry-sdk-metrics-1.9.1-alpha.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-sdk-logs.9.1-alpha\opentelemetry-sdk-logs-1.9.1-alpha.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-api-metrics.9.1-alpha\opentelemetry-api-metrics-1.9.1-alpha.jar;C:\Users\jackg\.m2\repository\org\asynchttpclient\async-http-client-netty-utils.12.3\async-http-client-netty-utils-2.12.3.jar;C:\Users\jackg\.m2\repository\io\netty\netty-codec-socks.1.60.Final\netty-codec-socks-4.1.60.Final.jar;C:\Users\jackg\.m2\repository\io\netty\netty-handler-proxy.1.60.Final\netty-handler-proxy-4.1.60.Final.jar;C:\Users\jackg\.m2\repository\io\netty\netty-transport-native-epoll.1.60.Final\netty-transport-native-epoll-4.1.60.Final-linux-x86_64.jar;C:\Users\jackg\.m2\repository\io\netty\netty-transport-native-kqueue.1.60.Final\netty-transport-native-kqueue-4.1.60.Final-osx-x86_64.jar;C:\Users\jackg\.m2\repository\org\reactivestreams\reactive-streams.0.3\reactive-streams-1.0.3.jar;C:\Users\jackg\.m2\repository\com\typesafe\netty\netty-reactive-streams.0.4\netty-reactive-streams-2.0.4.jar;C:\Users\jackg\.m2\repository\com\sun\activation\jakarta.activation.2.2\jakarta.activation-1.2.2.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-safari-driver.1.1\selenium-safari-driver-4.1.1.jar;C:\Users\jackg\.m2\repository\org\slf4j\slf4j-api.7.32\slf4j-api-1.7.32.jar;C:\Users\jackg\.m2\repository\com\google\code\gson\gson.8.8\gson-2.8.8.jar;C:\Users\jackg\.m2\repository\org\jsoup\jsoup.14.2\jsoup-1.14.2.jar;C:\Users\jackg\.m2\repository\com\github\docker-java\docker-java.2.12\docker-java-3.2.12.jar;C:\Users\jackg\.m2\repository\com\github\docker-java\docker-java-core.2.12\docker-java-core-3.2.12.jar;C:\Users\jackg\.m2\repository\com\github\docker-java\docker-java-api.2.12\docker-java-api-3.2.12.jar;C:\Users\jackg\.m2\repository\com\fasterxml\jackson\core\jackson-annotations.10.3\jackson-annotations-2.10.3.jar;C:\Users\jackg\.m2\repository\commons-io\commons-io.6\commons-io-2.6.jar;C:\Users\jackg\.m2\repository\org\apache\commons\commons-compress.21\commons-compress-1.21.jar;C:\Users\jackg\.m2\repository\commons-lang\commons-lang.6\commons-lang-2.6.jar;C:\Users\jackg\.m2\repository\com\fasterxml\jackson\core\jackson-databind.10.3\jackson-databind-2.10.3.jar;C:\Users\jackg\.m2\repository\com\fasterxml\jackson\core\jackson-core.10.3\jackson-core-2.10.3.jar;C:\Users\jackg\.m2\repository\org\bouncycastle\bcpkix-jdk15on.64\bcpkix-jdk15on-1.64.jar;C:\Users\jackg\.m2\repository\org\bouncycastle\bcprov-jdk15on.64\bcprov-jdk15on-1.64.jar;C:\Users\jackg\.m2\repository\org\slf4j\jcl-over-slf4j.7.30\jcl-over-slf4j-1.7.30.jar;C:\Users\jackg\.m2\repository\com\github\docker-java\docker-java-transport-httpclient5.2.12\docker-java-transport-httpclient5-3.2.12.jar;C:\Users\jackg\.m2\repository\com\github\docker-java\docker-java-transport.2.12\docker-java-transport-3.2.12.jar;C:\Users\jackg\.m2\repository\org\apache\httpcomponents\client5\httpclient5.0.3\httpclient5-5.0.3.jar;C:\Users\jackg\.m2\repository\org\apache\httpcomponents\core5\httpcore5.0.2\httpcore5-5.0.2.jar;C:\Users\jackg\.m2\repository\commons-codec\commons-codec.13\commons-codec-1.13.jar;C:\Users\jackg\.m2\repository\com\google\guava\failureaccess.0.1\failureaccess-1.0.1.jar;C:\Users\jackg\.m2\repository\com\google\guava\listenablefuture99.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;C:\Users\jackg\.m2\repository\com\google\code\findbugs\jsr305.0.2\jsr305-3.0.2.jar;C:\Users\jackg\.m2\repository\org\checkerframework\checker-qual.8.0\checker-qual-3.8.0.jar;C:\Users\jackg\.m2\repository\com\google\errorprone\error_prone_annotations.5.1\error_prone_annotations-2.5.1.jar;C:\Users\jackg\.m2\repository\com\google\j2objc\j2objc-annotations.3\j2objc-annotations-1.3.jar;C:\Users\jackg\.m2\repository\org\brotli\dec[=14=].1.2\dec-0.1.2.jar;C:\Users\jackg\.m2\repository\org\apache\commons\commons-lang3.12.0\commons-lang3-3.12.0.jar;C:\Users\jackg\.m2\repository\org\apache\commons\commons-text.9\commons-text-1.9.jar;C:\Users\jackg\.m2\repository\commons-beanutils\commons-beanutils.9.4\commons-beanutils-1.9.4.jar;C:\Users\jackg\.m2\repository\commons-logging\commons-logging.2\commons-logging-1.2.jar;C:\Users\jackg\.m2\repository\commons-collections\commons-collections.2.2\commons-collections-3.2.2.jar;C:\Users\jackg\.m2\repository\org\apache\commons\commons-collections4.4\commons-collections4-4.4.jar -p "C:\Users\jackg\.m2\repository\org\openjfx\javafx-base.0.1\javafx-base-11.0.1-win.jar;C:\Users\jackg\.m2\repository\io\netty\netty-transport-classes-kqueue.1.70.Final\netty-transport-classes-kqueue-4.1.70.Final.jar;C:\Users\jackg\.m2\repository\org\controlsfx\controlsfx.1.0\controlsfx-11.1.0.jar;C:\Users\jackg\.m2\repository\net\synedra\validatorfx[=14=].1.13\validatorfx-0.1.13.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-sdk.9.1\opentelemetry-sdk-1.9.1.jar;C:\Users\jackg\.m2\repository\org\openjfx\javafx-graphics.0.2\javafx-graphics-11.0.2-win.jar;C:\Users\jackg\.m2\repository\org\openjfx\javafx-fxml.0.2\javafx-fxml-11.0.2-win.jar;C:\Users\jackg\.m2\repository\com\beust\jcommander.81\jcommander-1.81.jar;C:\Users\jackg\.m2\repository\org\openjfx\javafx-controls.0.2\javafx-controls-11.0.2-win.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-chromium-driver.1.1\selenium-chromium-driver-4.1.1.jar;C:\Users\jackg\.m2\repository\io\netty\netty-transport-classes-epoll.1.70.Final\netty-transport-classes-epoll-4.1.70.Final.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-api.1.1\selenium-api-4.1.1.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-json.1.1\selenium-json-4.1.1.jar;C:\Users\jackg\.m2\repository\net\java\dev\jna\jna.8.0\jna-5.8.0.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-sdk-extension-autoconfigure-spi.9.1\opentelemetry-sdk-extension-autoconfigure-spi-1.9.1.jar;C:\Users\jackg\.m2\repository\net\jodah\failsafe.4.4\failsafe-2.4.4.jar;C:\Users\jackg\.m2\repository\io\netty\netty-transport-native-unix-common.1.70.Final\netty-transport-native-unix-common-4.1.70.Final.jar;C:\Users\jackg\.m2\repository\io\netty\netty-codec-http.1.70.Final\netty-codec-http-4.1.70.Final.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-sdk-extension-autoconfigure.9.1-alpha\opentelemetry-sdk-extension-autoconfigure-1.9.1-alpha.jar;C:\Users\jackg\.m2\repository\net\bytebuddy\byte-buddy.12.2\byte-buddy-1.12.2.jar;C:\Users\jackg\.m2\repository\io\netty\netty-buffer.1.70.Final\netty-buffer-4.1.70.Final.jar;C:\Users\jackg\.m2\repository\io\github\bonigarcia\webdrivermanager.0.3\webdrivermanager-5.0.3.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-sdk-common.9.1\opentelemetry-sdk-common-1.9.1.jar;C:\Users\jackg\.m2\repository\org\apache\commons\commons-exec.3\commons-exec-1.3.jar;C:\Users\jackg\.m2\repository\io\netty\netty-common.1.70.Final\netty-common-4.1.70.Final.jar;C:\Users\jackg\.m2\repository\org\kordamp\ikonli\ikonli-javafx.2.0\ikonli-javafx-12.2.0.jar;C:\Users\jackg\.m2\repository\com\dlsc\formsfx\formsfx-core.4.2\formsfx-core-11.4.2.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-sdk-trace.9.1\opentelemetry-sdk-trace-1.9.1.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-api.9.1\opentelemetry-api-1.9.1.jar;C:\Users\jackg\.m2\repository\org\openjfx\javafx-web.0.2\javafx-web-11.0.2-win.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-context.9.1\opentelemetry-context-1.9.1.jar;C:\Users\jackg\.m2\repository\com\google\guava\guava.1.1-jre\guava-30.1.1-jre.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-chrome-driver.1.1\selenium-chrome-driver-4.1.1.jar;C:\Users\jackg\.m2\repository\com\opencsv\opencsv.5.2\opencsv-5.5.2.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-support.1.1\selenium-support-4.1.1.jar;C:\Users\jackg\.m2\repository\org\kordamp\bootstrapfx\bootstrapfx-core[=14=].4.0\bootstrapfx-core-0.4.0.jar;C:\Users\jackg\.m2\repository\org\openjfx\javafx-media.0.2\javafx-media-11.0.2-win.jar;C:\Users\jackg\.m2\repository\eu\hansolo\tilesfx.48\tilesfx-11.48.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-http.1.1\selenium-http-4.1.1.jar;C:\Users\jackg\.m2\repository\org\asynchttpclient\async-http-client.12.3\async-http-client-2.12.3.jar;C:\Users\jackg\.m2\repository\io\opentelemetry\opentelemetry-semconv.9.1-alpha\opentelemetry-semconv-1.9.1-alpha.jar;C:\Users\jackg\.m2\repository\io\ous\jtoml.0.0\jtoml-2.0.0.jar;C:\Users\jackg\.m2\repository\org\seleniumhq\selenium\selenium-remote-driver.1.1\selenium-remote-driver-4.1.1.jar;C:\Users\jackg\.m2\repository\io\netty\netty-transport.1.70.Final\netty-transport-4.1.70.Final.jar;C:\Users\jackg\.m2\repository\org\kordamp\ikonli\ikonli-core.2.0\ikonli-core-12.2.0.jar;D:\Users\jackg\Desktop\Java Code\Ethermine Tracker JavaFX\target\classes" -m com.traptricker.etherminetrackerjavafx/com.traptricker.etherminetrackerjavafx.Main
Dec 30, 2021 9:22:25 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 17 by JavaFX runtime of version 11.0.1
Starting program...
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 95.0.4638.17 (a9d0719444d4b035e284ed1fce73bf6ccd789df2-refs/branch-heads/4638@{#178}) on port 63013
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1640917354.440][WARNING]: This version of ChromeDriver has not been tested with Chrome version 96.
Dec 30, 2021 9:22:34 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Dec 30, 2021 9:22:34 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Found exact CDP implementation for version 96
Starting ChromeDriver 95.0.4638.17 (a9d0719444d4b035e284ed1fce73bf6ccd789df2-refs/branch-heads/4638@{#178}) on port 53163
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1640917356.435][WARNING]: This version of ChromeDriver has not been tested with Chrome version 96.
Dec 30, 2021 9:22:36 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Dec 30, 2021 9:22:36 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Found exact CDP implementation for version 96
Exception in thread "Thread-3" java.lang.NullPointerException
    at com.traptricker.etherminetrackerjavafx/com.traptricker.etherminetrackerjavafx.SeleniumScrapper.setUpWebdriver(SeleniumScrapper.java:31)
    at com.traptricker.etherminetrackerjavafx/com.traptricker.etherminetrackerjavafx.Controller.trackEthermine(Controller.java:54)
    at java.base/java.lang.Thread.run(Thread.java:829)

您需要注意以下几点:

  • 您已经下载了 并以以下方式访问它:

    System.setProperty("webdriver.chrome.driver", "src/main/extras/chromedriver.exe");
    
  • 此外,您还通过 WebDriverManager 下载 作为:

    WebDriverManager.chromedriver().setup();
    

因此,您的程序可以访问 ChromeDriver 的多个实例。因此你看到:

Exception in thread "Thread-3" java.lang.NullPointerException

解决方案

您只需要使用一个 ChromeDriver 实例,要么是下载的版本,要么是通过 WebDriverManager[=15= 下载并访问的版本]