尝试使用 Selenium 合并 DesiredCapativity 时出现 java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.merge(Capability)

2023-12-27

当我尝试推出新产品时Selenium/Firefox实例与DesiredCapabilities and FirfoxOptions我得到以下代码:

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;


}

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

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

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

我究竟做错了什么?

Thanks!

UPDATE:

在 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();
}

Thanks!!


这个错误信息...

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

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


merge()

merge() https://selenium.dev/selenium/docs/api/java/org/openqa/selenium/MutableCapabilities.html#merge-org.openqa.selenium.Capabilities-定义于可变能力 https://selenium.dev/selenium/docs/api/java/org/openqa/selenium/MutableCapabilities.html as:

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 https://stackoverflow.com/questions/54459701/what-is-selenium-and-what-is-webdriver/54482491#54482491 v3.7.0:

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

Maven 依赖

此外,您需要确保已添加一个<dependency> for Selenium如下:

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

Note: <dependency> for 硒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 https://www.oracle.com/technetwork/java/javase/downloads/index.html.
  • Selenium升级到当前级别版本 3.141.59 https://docs.seleniumhq.org/download/.
  • Upgrade Gecko驱动程序 to Gecko驱动程序 v0.26.0 https://github.com/mozilla/geckodriver/releases/tag/v0.26.0 level.
  • Upgrade Firefox版本为火狐浏览器 v72.0 levels.
  • 如果你的基地网页客户端版本太旧,然后通过卸载雷沃卸载程序 https://www.revouninstaller.com/revo_uninstaller_free_download.html并安装最新的 GA 和发布版本网页客户端.
  • Take a 系统重启.
  • Perform mvn clean, mvn build and mvn test
  • 执行你的Test作为非 root 用户。

Note: 的情况下传递依赖 https://en.wikipedia.org/wiki/Transitive_dependency你可能必须删除MAVEN_HOME i.e. ~\.m2子目录。


参考

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

  • 如何使用 xvfb 将 Chrome 驱动程序服务与无头所需的功能合并? https://stackoverflow.com/questions/47671884/how-to-merge-chrome-driver-service-with-desired-capabilities-for-headless-using/47672910#47672910
  • 如何通过 Java 使用 Selenium 将功能和选项传递到 Firefoxdriver https://stackoverflow.com/questions/57494915/how-to-pass-the-capabilities-and-options-into-firefoxdriver-using-selenium-throu/57495469#57495469
  • 如何解决“ChromeDriver(功能)构造函数已弃用”和 WebDriverException:ChromeDriver 和 Chrome 出现超时错误 https://stackoverflow.com/questions/50841374/how-to-address-the-constructor-chromedrivercapabilities-is-deprecated-and-we/50841549#50841549
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试使用 Selenium 合并 DesiredCapativity 时出现 java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.merge(Capability) 的相关文章

随机推荐