java.lang.NoSuchMethodError:尝试使用Selenium合并DesiredCapabilities时,org.openqa.selenium.firefox.FirefoxOptions.merge(Capabilities) [英] java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.merge(Capabilities) when trying to merge DesiredCapabilities using Selenium

查看:164
本文介绍了java.lang.NoSuchMethodError:尝试使用Selenium合并DesiredCapabilities时,org.openqa.selenium.firefox.FirefoxOptions.merge(Capabilities)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I try launching new Selenium/Firefox instance with DesiredCapabilities and FirfoxOptions I get the following code:

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文件包含以下条目:

My POM file contains the following entries:

<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>

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

Previously, I had version 3.5.2 of org.seleniumhq.selenium in POM which does not support merge functionality. However, when I tried launching Selenium with version 3.5.2 using following code:

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).

Neither version 3.11.0 or version 3.5.2 is working (I also tried 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()基本上执行以下操作:

The method getTMPFirefoxProfile() basically does the following:

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

谢谢!

推荐答案

此错误消息...

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

...表示您添加的类中没有定义为merge()的方法 .

...implies that there was no such method defined as merge() within the classes you have added.

中定义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.


MutableCapabilities

MutableCapabilities已在 Selenium v3中添加. 7.0:


MutableCapabilities

MutableCapabilities was added in Selenium v3.7.0:

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


Maven依赖

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


Maven dependency

Further, you need to ensure you have added a single <dependency> for Selenium as follows:

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

注意:硒Java <dependency>隐式包含番石榴<dependency>.

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);


其他建议

确保:


Additional recommendations

Ensure that:

  • JDK 已升级到当前级别 JDK 8u232 .
  • 已升级到当前级别版本3.141.59 .
  • GeckoDriver 升级到 GeckoDriver v0.26.0 级.
  • Firefox 版本升级到 Firefox v72.0 级别.
  • 如果您的基本 Web客户端版本太旧,请通过来卸载. Revo Uninstaller 并安装最新版本的 Web客户端 GA和发行版.
  • 进行系统重启.
  • 执行mvn cleanmvn buildmvn test
  • 以非root用户身份执行Test.
  • JDK is upgraded to current levels JDK 8u232.
  • Selenium is upgraded to current levels Version 3.141.59.
  • Upgrade GeckoDriver to GeckoDriver v0.26.0 level.
  • Upgrade Firefox version to Firefox v72.0 levels.
  • If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • Perform mvn clean, mvn build and mvn test
  • Execute your Test as a non-root user.

注意:对于传递依赖,您可能有删除MAVEN_HOME~\.m2子目录.

Note: In case of transitive dependency you may have to delete the MAVEN_HOME i.e. ~\.m2 sub directory.


参考

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


Reference

You can find a couple of relevant discussion in:

  • How to Merge Chrome driver service with desired capabilities for headless using xvfb?
  • How to pass the capabilities and options into Firefoxdriver using Selenium through Java
  • How to address "The constructor ChromeDriver(Capabilities) is deprecated" and WebDriverException: Timed out error with ChromeDriver and Chrome

这篇关于java.lang.NoSuchMethodError:尝试使用Selenium合并DesiredCapabilities时,org.openqa.selenium.firefox.FirefoxOptions.merge(Capabilities)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆