-1

当我尝试启动新Selenium/Firefox实例时DesiredCapabilitiesFirfoxOptions我得到以下代码:

Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.merge(Lorg/openqa/selenium/Capabilities;)Lorg/openqa/selenium/firefox/FirefoxOptions;

我正在使用以下代码:

public WebDriver getDriver() throws MalformedObjectNameException, InstanceNotFoundException, ReflectionException, InterruptedException
{

    System.setProperty("webdriver.gecko.driver", GlobalVar.geckdriverExecutableFilePath);

    //DesiredCapabilities capabilities = new DesiredCapabilities();



    DesiredCapabilities dc = DesiredCapabilities.firefox();


    if (proxyPOJO != null) {


        Proxy proxy = new Proxy();
        proxy.setHttpProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());
        proxy.setFtpProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());
        proxy.setSslProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());


        dc.setCapability(CapabilityType.PROXY, proxy);
    }

    FirefoxOptions opt = new FirefoxOptions();
    opt.merge(dc);


    opt.addPreference("dom.popup_maximum", 200);
    opt.addPreference("dom.webnotifications.enabled", false); 


    opt.merge(capabilities);


    WebDriver driver = WebDriverX.getNewFireFoxWebDriver(opt); // Basically calls: driver = new FirefoxDriver(firefoxOptions);  


    return driver;


}

我的POM文件包含以下条目:

<dependencies>

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

</dependencyManagement>

<dependencies>

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

 <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>26.0-jre</version>
 </dependency>

以前,我org.seleniumhq.selenium在 POM 中拥有不支持merge功能的 3.5.2 版本。但是,当我尝试3.5.2使用以下代码启动带有版本的 Selenium 时:

System.setProperty("webdriver.gecko.driver", GlobalVar.geckdriverExecutableFilePath);
DesiredCapabilities capabilities = new DesiredCapabilities();

    if (proxyPOJO != null) {

        Proxy proxy = new Proxy();
        proxy.setHttpProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());
        proxy.setFtpProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());
        proxy.setSslProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());


        capabilities.setCapability(CapabilityType.PROXY, proxy);
    }
FirefoxOptions firefoxOptions = new FirefoxOptions(capabilities);
WebDriver driver = WebDriverX.getNewFireFoxWebDriver(firefoxOptions); 

我得到以下异常:

NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.<init>(Lorg/openqa/selenium/Capabilities;)V

我已经geckodriver.exe安装了最新版本。

版本 3.11.0 或版本 3.5.2 都不起作用(我也尝试过 3.8.2)。

我究竟做错了什么?

谢谢!

更新:

使用 3.11.0 版本,我得到以下堆栈跟踪:

Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.merge(Lorg/openqa/selenium/Capabilities;)Lorg/openqa/selenium/firefox/FirefoxOptions;
    at webdriverX.WebDriverProfile.getTMPFirefoxProfile(WebDriverProfile.java:259)
    at s.SPage.scrapeS(SPage.java:36)
    at n.NMain.main(NMain.java:27)

对于 3.5.2 版本,以下是堆栈跟踪:

Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.<init>(Lorg/openqa/selenium/Capabilities;)V
    at webdriverX.WebDriverProfile.getTMPFirefoxProfile(WebDriverProfile.java:232)
    at s.SPage.scrapeS(SPage.java:36)
    at n.NMain.main(NMain.java:27)

该方法getTMPFirefoxProfile()主要执行以下操作:

if (firefoxOptions != null) {
    driver = new FirefoxDriver(firefoxOptions);
} else {
                driver = new FirefoxDriver();
}

谢谢!!

4

1 回答 1

-1

此错误消息...

Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.merge(Lorg/openqa/selenium/Capabilities;)Lorg/openqa/selenium/firefox/FirefoxOptions;

...意味着在您添加的类中没有定义这样的方法。merge()


合并()

merge()MutableCapabilities中定义为:

public MutableCapabilities merge(Capabilities extraCapabilities)
    Merge the extra capabilities provided into this DesiredCapabilities instance. If capabilities with the same name exist in this instance, they will be overridden by the values from the extraCapabilities object.

Specified by:
    merge in interface Capabilities

Parameters:
    extraCapabilities - Additional capabilities to be added.

Returns:
    The DesiredCapabilities instance after the merge.

可变能力

在Selenium v ​​3.7.0 中添加了MutableCapabilities:

v3.7.0
======
* Migrated from using `DesiredCapabilities` to either
  `MutableCapabilities` or (preferably) `ImmutableCapabilities`.

Maven依赖

此外,您需要确保<dependency>Selenium添加了一个,如下所示:

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

注意<dependency>对于selenium-java隐式还包括 <dependency>for guava


示例代码

System.setProperty("webdriver.gecko.driver", "gecko/linux/geckodriver");

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.no_proxies_on", "localhost");
profile.setPreference("javascript.enabled", true);

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);

FirefoxOptions options = new FirefoxOptions();
options.merge(capabilities);
options.setLogLevel(Level.FINEST);
options.addPreference("browser.link.open_newwindow", 3);
options.addPreference("browser.link.open_newwindow.restriction", 0);

WebDriver driver = new FirefoxDriver(options);

其他建议

确保这件事:

  • JDK升级到当前级别JDK 8u232
  • Selenium升级到当前级别版本 3.141.59
  • 将GeckoDriver升级到GeckoDriver v0.26.0级别。
  • 将Firefox版本升级到Firefox v72.0级别。
  • 如果您的基本Web Client版本太旧,请通过Revo Uninstaller卸载它并安装最新的 GA 和已发布版本的Web Client
  • 重新启动系统
  • 执行mvn clean,mvn buildmvn test
  • Test以非 root 用户身份执行。

注意:在传递依赖的情况下,您可能必须删除MAVEN_HOMEie~\.m2子目录。


参考

您可以在以下位置找到一些相关的讨论:

于 2020-02-05T17:21:03.763 回答