Selenium 3.3.1和FirefoxDriver的依赖性错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium 3.3.1和FirefoxDriver的依赖性错误相关的知识,希望对你有一定的参考价值。

我使用geckodriver v0.15.0(最新版本)和Firefox 52.0.1(64位)

这是我的代码:

public static void main(String[] args) {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com");
    System.out.println("Page title is: " + driver.getTitle());
    driver.quit();
}

以下是我的pom中Selenium的依赖关系(Selenium的最新版本):

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-firefox-driver</artifactId>
    <version>3.3.1</version>
    <scope>test</scope>
</dependency>  
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-support</artifactId>
    <version>3.3.1</version>
    <scope>test</scope>
</dependency> 

和运行时的异常:

Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.net.PortProber.waitForPortUp(IILjava/util/concurrent/TimeUnit;)V
at org.openqa.selenium.firefox.GeckoDriverService.waitUntilAvailable(GeckoDriverService.java:73)
at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:166)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:644)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:218)

如果我添加这个依赖:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-remote-driver</artifactId>
    <version>3.3.1</version>
</dependency> 

我有另一个例外:

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/interactions/Interactive

缺少什么依赖?谢谢。

UPDATE

mvn dependency:treehtmlunit-driver 2.25,selenium-remote-driver 3.3.1,selenium-firefox-driver 3.3.1,selenium-support 3.3.1

[INFO] +- org.seleniumhq.selenium:htmlunit-driver:jar:2.25:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-api:jar:2.53.1:compile
[INFO] |  - net.sourceforge.htmlunit:htmlunit:jar:2.21:compile
[INFO] |     +- xalan:xalan:jar:2.7.2:compile
[INFO] |     |  - xalan:serializer:jar:2.7.2:compile
[INFO] |     +- org.apache.commons:commons-lang3:jar:3.4:compile
[INFO] |     +- org.apache.httpcomponents:httpmime:jar:4.5.2:compile
[INFO] |     +- net.sourceforge.htmlunit:htmlunit-core-js:jar:2.17:compile
[INFO] |     +- net.sourceforge.htmlunit:neko-htmlunit:jar:2.21:compile
[INFO] |     |  - xerces:xercesImpl:jar:2.11.0:compile
[INFO] |     |     - xml-apis:xml-apis:jar:1.4.01:compile
[INFO] |     +- net.sourceforge.cssparser:cssparser:jar:0.9.18:compile
[INFO] |     |  - org.w3c.css:sac:jar:1.3:compile
[INFO] |     +- commons-io:commons-io:jar:2.4:compile
[INFO] |     - org.eclipse.jetty.websocket:websocket-client:jar:9.4.1.v20170120:compile
[INFO] |        +- org.eclipse.jetty:jetty-util:jar:9.4.1.v20170120:compile
[INFO] |        +- org.eclipse.jetty:jetty-io:jar:9.4.1.v20170120:compile
[INFO] |        +- org.eclipse.jetty:jetty-client:jar:9.4.1.v20170120:compile
[INFO] |        |  - org.eclipse.jetty:jetty-http:jar:9.4.1.v20170120:compile
[INFO] |        - org.eclipse.jetty.websocket:websocket-common:jar:9.4.1.v20170120:compile
[INFO] |           - org.eclipse.jetty.websocket:websocket-api:jar:9.4.1.v20170120:compile
[INFO] +- org.seleniumhq.selenium:selenium-remote-driver:jar:3.3.1:compile
[INFO] |  +- cglib:cglib-nodep:jar:3.2.4:compile
[INFO] |  +- org.apache.commons:commons-exec:jar:1.3:compile
[INFO] |  - net.java.dev.jna:jna-platform:jar:4.1.0:compile
[INFO] |     - net.java.dev.jna:jna:jar:4.2.2:compile
[INFO] +- org.seleniumhq.selenium:selenium-firefox-driver:jar:3.3.1:compile
[INFO] - org.seleniumhq.selenium:selenium-support:jar:3.3.1:compile

更新2

AS:64位

答案

这是必需的依赖项(我也清理了.m2目录):

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.6</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-remote-driver</artifactId>
    <version>3.3.1</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-api</artifactId>
    <version>3.3.1</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-firefox-driver</artifactId>
    <version>3.3.1</version>
</dependency>
另一答案

也许你有jar版本冲突错误。当我在我的应用程序中遇到该错误时,我发现它指的是旧的Selenium jar。为解决此问题,我删除了旧jar并使用Selenium 3罐重建项目。

确保为项目添加了正确的库。

命令mvn dependency:tree可以帮助你。

注意:当您使用测试范围时,它意味着依赖关系仅适用于测试编译和执行阶段,因此您必须将代码放在Test方法中或删除范围标记

希望这可以帮助。

另一答案

检查您是否具有与其他依赖项相同版本的selenium-java依赖项

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.3.1</version>
</dependency>
另一答案

System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\geckodriver.exe");添加到您的代码中。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CheckFireFox {
    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        System.out.println("Page title is: " + driver.getTitle());
        driver.quit();
    }

}

pom.hml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>StackOverFlow</groupId>
    <artifactId>StackOverFlow</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.3.1</version>
        </dependency>
    </dependencies>
</project>

控制台输出:

Mar 27, 2017 2:39:01 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Page title is: Google
另一答案

简单的解决方案导入selenium standalone并导入org.openqa.selenium.firefox.FirefoxDriver;

对我来说它有效

另一答案
  1. 更新selenium版本
  2. 下载geckodriver并设置系统路径,例如“F: geckodriver-v0.20.1-win64”
  3. 将以下代码段添加到现有代码中DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability(“marionette”,true);

现在运行脚本,它将对你有用,希望这会有所帮助。

以上是关于Selenium 3.3.1和FirefoxDriver的依赖性错误的主要内容,如果未能解决你的问题,请参考以下文章

Java Selenium 3.3.1 和 geckodriver 0.15.0 不在 Windows 10 上等待

selenium源码通读·9 |webdriver/common/desired_capabilities.py-DesiredCapabilities类分析

Selenium学习Selenium 总结

selenium ide和selenium python的区别

Selenium02篇python+selenium实现Web自动化:鼠标操作和键盘操作!

Maven 存储库中的 Selenium Maven 工件 selenium-api 和 selenium-support 有啥区别?