【问题标题】:Merging DesiredCapabilities with FirefoxOptions in Selenium Webdriver Results in java.lang.NoSuchMethodError在 Selenium Webdriver 中将 DesiredCapabilities 与 FirefoxOptions 合并导致 java.lang.NoSuchMethodError
【发布时间】:2020-05-22 02:26:16
【问题描述】:

当我尝试启动 Selenium WebDriver 时,我得到:NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.merge

我在main 中的代码调用了一个方法:

WebDriver driver = new WebDriverProfile().getTMPFirefoxProfile(null); // Parameter is optional

我正在详细发布我的代码,希望也许有人能够提供建议以指导我朝着正确的方向前进。

堆栈跟踪:

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:286)
    at n.NMain.main(NMain.java:22)

调用getTMPFirefoxProfile(ProxyPOJO proxyPOJO)调用代码:

public WebDriver getTMPFirefoxProfile(ProxyPOJO proxyPOJO) throws InterruptedException, MalformedObjectNameException, InstanceNotFoundException, ReflectionException {

    System.setProperty("webdriver.gecko.driver", GlobalVar.geckdriverExecutableFilePath); // Verified path is correct via syso


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


    DesiredCapabilities dc = DesiredCapabilities.firefox();


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



    return driver;
}

调用:getNewFireFoxWebDriver(FirefoxOptions firefoxOptions) 调用此代码:

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

 return driver;

我已经问过a similar question,但我现在已经尝试了近 10 种不同的建议 - 没有一个能解决我的问题。

下面我将概述我为尝试解决问题所采取的步骤:

1) 右键单击​​项目 -> Maven clean -> Maven build -> Maven test(我分别尝试了每个,然后一个接一个)

2) 点击项目 -> 清理

3) 确认 Firefox 已更新至最新版本(版本 72)

4) 从项目 POM 中删除库 guava(唯一建议的冲突)

5) 关闭并重新启动 Eclipse

6) 删除所有运行配置,从头开始运行项目

7) 重新下载 GeckoDriver v0.26.0 以确保我安装了最低版本

8) 我确定我的 POM 文件包含 Selenium 依赖版本 3.141.591 其中包含 merge 方法

我别无选择。接下来我该尝试什么?

下面是项目POM文件的内容:

<dependencies>

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

    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>3.9.2</version>
    </dependency>

    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>3.9.2</version>
        <classifier>models</classifier>
    </dependency>

    <dependency>
        <groupId>uk.ac.abdn</groupId>
        <artifactId>SimpleNLG</artifactId>
        <version>4.4.8</version>
    </dependency>

</dependencies>

我在该项目的构建路径中链接了另外两个项目。从一个项目中,我可以毫无问题地致电getTMPFirefoxProfile(ProxyPOJO proxyPOJO)。从另一个,我得到与当前项目相同的错误。这是什么意思?我分析了另一个项目,没有看到任何冲突的依赖项。

我花了一整天的时间来解决这个问题,但我完全迷失了。

有什么想法吗?

谢谢!

【问题讨论】:

    标签: java eclipse maven selenium selenium-webdriver


    【解决方案1】:

    在您的previous question 中涵盖了所有可能的问题和解决方案之后,您现在似乎已经很接近了。

    此外,我建议使用 为Java 安装Selenium 库,您应该首先在您的项目pom.xml 中添加&lt;selenium-java&gt; 依赖项,因为它支持使用所有支持Selenium 的浏览器运行您的自动化项目。:

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

    更进一步,如果你只想在 Firefox 中运行测试,你可以替换为 selenium-firefox-driver 依赖:

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

    【讨论】:

    • @Debanjan_B 我将selenium-java 添加到POM 文件无济于事。我怀疑Java 正在加载以前版本的 Selenium,因为项目在构建路径中的传递依赖。但是,所有其他项目都编译为版本3.141.59。我怎样才能确认这一点?此外,完全相同的方法调用在另一个项目中完美地工作 - 另一个项目在其构建路径中具有所有相同的项目(+其他项目)。如果这确实是一个传递依赖的问题,它不会平等地影响所有项目吗?为什么会从一个而不是另一个抛出异常?感谢您的帮助!
    • @Debanjan_B 我想补充一点,我通过 Dependency HierarchyMaven 中确认Selenium 符合3.141.59 版本。 Maven 在此版本旁边显示 [compile]Maven 是否有可能在错误版本旁边显示 compile?如果是,我怎么知道?另外,我知道一个项目(通过当前项目的构建路径间接引用)有一个名为 [AShot] 的库,它依赖于Selenium Remote Driver version 2.5.3。但是,Maven 说它是omitted for conflict with version 3.141.59。此外,对getTMPFirefoxProfile() 的调用实际上在该项目中有效..
    • @Debanjan_B 经过多次故障排除后,我终于找到了解决方案。我创建了一个名为Project_Libs 的新库在库中我添加了Selenium 版本3.141.59 的Jar 文件。右键单击项目 -> 属性 -> Java 构建路径并将Project_Libs 作为库添加到项目中。但是,仅此一项并不能解决我的问题。在Order and Export 面板中,我必须将Project_Libs 移动到另一个名为Libs 的库上方(在Maven 库下方)。我还必须手动将 Guava 罐子添加到这个库中。现在它起作用了!以后有没有办法避免这一切?
    • @AlanCook 如果您有多个基于 maven 的项目的transitive dependency,而没有看到错误日志/异常或不了解它们,那么我进一步评论不是一个好主意。但是,对于特定环境 (os),共享 jar 可能会损坏。
    • @AlanCook 关于您当前的解决方案:我也不会对此感到满意,因为我怀疑更多冲突会以NoSuchMethodErrorNoClassDefFoundError 的形式出现,而您最不希望它们出现。无论如何,很高兴知道您暂时解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2020-05-21
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多