java.lang.NoClassDefFoundError 在 docker 中使用 FirefoxDriver(Selenium) 和 alpine 图像

java.lang.NoClassDefFoundError while using FirefoxDriver(Selenium) in docker with alpine image

尝试使用 FireFox 驱动程序对网络浏览器进行一些操作,但是当我docker编辑我的应用程序时,我遇到了我发布的错误。

使用 Selenium、Firefox 驱动程序和 maven:3.6.1-jdk-8-alpine 图像,上面带有 fierfox-esr 和 xvfb

一段程序:

@Scheduled(fixedRate = 20000)
    public void main() throws InterruptedException, IOException {
        System.setProperty("webdriver.gecko.driver", "Gecko/geckodriver");

        QueueEntity queue = QueueDAO.next();
        if (queue != null) {
            WebDriver driver = new FirefoxDriver();

最后一行导致错误:

1637168554124   geckodriver INFO    Listening on 127.0.0.1:24425
2021-11-17T17:02:34.134011600Z 1637168554133    mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "--marionette" "-no-remote" "-profile" "/tmp/rust_mozprofileUkF2hH"
2021-11-17T17:02:36.256633700Z 1637168556256    Marionette  INFO    Listening on port 45371
2021-11-17T17:02:36.347875300Z 1637168556347    Marionette  WARN    TLS certificate errors will be ignored for this session
2021-11-17T17:02:36.413224000Z 2021-11-17 17:02:36.412  INFO 54 --- [ null to remote] o.o.selenium.remote.ProtocolHandshake    : Detected dialect: W3C
2021-11-17T17:02:36.414915900Z 2021-11-17 17:02:36.414 ERROR 54 --- [   scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task
2021-11-17T17:02:36.415000400Z 
2021-11-17T17:02:36.415009800Z java.lang.NoClassDefFoundError: org/openqa/selenium/remote/RemoteWebElement
2021-11-17T17:02:36.415013100Z  at org.openqa.selenium.remote.internal.JsonToWebElementConverter.apply(JsonToWebElementConverter.java:63) ~[selenium-remote-driver-3.141.59.jar:na]
2021-11-17T17:02:36.415016400Z  at com.google.common.collect.Maps.transformEntry(Maps.java:1955) ~[guava-25.0-jre.jar:na]
2021-11-17T17:02:36.415019300Z  at com.google.common.collect.Maps.getValue(Maps.java:1996) ~[guava-25.0-jre.jar:na]
2021-11-17T17:02:36.415022100Z  at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:222) ~[selenium-remote-driver-3.141.59.jar:na]
2021-11-17T17:02:36.415025000Z  at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131) ~[selenium-remote-driver-3.141.59.jar:na]
2021-11-17T17:02:36.415028100Z  at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:147) ~[selenium-firefox-driver-3.141.59.jar:na]
2021-11-17T17:02:36.415030900Z  at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125) ~[selenium-firefox-driver-3.141.59.jar:na]
2021-11-17T17:02:36.415033800Z  at staff.parser.scheduler.loopedMain.main(loopedMain.java:29) ~[classes/:na]
2021-11-17T17:02:36.415036600Z  at sun.reflect.GeneratedMethodAccessor41.invoke(Unknown Source) ~[na:na]
2021-11-17T17:02:36.415048800Z  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
2021-11-17T17:02:36.415052300Z  at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
2021-11-17T17:02:36.415055500Z  at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.3.12.jar:5.3.12]
2021-11-17T17:02:36.415060700Z  at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.3.12.jar:5.3.12]
2021-11-17T17:02:36.415065500Z  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_212]
2021-11-17T17:02:36.415070500Z  at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_212]
2021-11-17T17:02:36.415074200Z  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_212]
2021-11-17T17:02:36.415077800Z  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_212]
2021-11-17T17:02:36.415080700Z  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_212]
2021-11-17T17:02:36.415083500Z  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_212]
2021-11-17T17:02:36.415086300Z  at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
2021-11-17T17:02:36.415089000Z 

使用 maven

来自 pom.xml 的依赖项:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.6.0.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-common</artifactId>
            <version>2.0b1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

docker(高山)运行

Docker文件:

FROM maven:3.6.1-jdk-8-alpine
COPY ./src /app/src
COPY ./Gecko /app/Gecko
COPY pom.xml /app
WORKDIR /app
EXPOSE 3000
RUN mvn package
Run apk update
Run apk add xvfb
Run apk add firefox-esr
Run Xvfb :9 &
CMD ["mvn", "spring-boot:run"]

同时将 Display=:9 指定为环境变量

很高兴得到任何帮助^_^

尽管您提到了添加以下依赖项:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-api</artifactId>
    <version>4.0.0</version>
</dependency>
    

但是根据您的程序正在寻找的错误堆栈跟踪:

  • selenium-remote-driver-3.141.59.jar:na
  • selenium-remote-driver-3.141.59.jar:na
  • selenium-firefox-driver-3.141.59.jar:na

因此,找不到 class 定义 ,您会看到错误 java.lang.NoClassDefFoundError

检查您的 dockered 应用程序配置以解决 Selenium v3.141.59v4.0.0

之间的冲突