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
问题描述
当我尝试使用DesiredCapabilities
和FirfoxOptions
启动新的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.
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 clean
,mvn build
和mvn 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
andmvn 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:
- 如何要使用xvfb将具有所需功能的Chrome驱动程序服务合并到无头的位置?
- 如何通过Java使用Selenium将功能和选项传递给Firefoxdriver
- 如何解决构造函数ChromeDriver(Capabilities)已弃用"和WebDriverException:ChromeDriver和Chrome超时错误
- 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屋!