无法运行3.8.1版本的selenium Web驱动程序项目

时间:2018-03-03 14:15:26

标签: selenium selenium-webdriver webdriver selenium-chromedriver webdriver-io

无法使用番石榴21和22运行网络driver 3.8.1 geckodriver 0.19.1。 Firefox版本58.0.2(64位) 得到错误:

java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V

在iMac上运行

System.setProperty("webdriver.gecko.driver", "//Users//(username)//Downloads//engage-test//engage-test-common//exes//geckodriver");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
driver= new FirefoxDriver(capabilities);

1 个答案:

答案 0 :(得分:1)

错误说明了一切:

java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V

班级 NoSuchMethodError

public class NoSuchMethodError 扩展 IncompatibleClassChangeError ,根据 Java Docs ,如果应用程序被抛出则会被抛出尝试调用类的指定方法(静态或实例),并且该类不再具有该方法的定义。通常,编译器会捕获此错误,并且只有在类的定义发生不兼容更改时才会在运行时发生此错误。

解决方案

执行以下步骤:

  • 提供绝对路径时,请使用双反斜杠(\\)或单斜杠(/)。两者都是等价的。因此,您需要更新System.setProperty(),如下所示:

    System.setProperty("webdriver.gecko.driver", "/Users/<username>/Downloads/engage-test/engage-test-common/exes/geckodriver"); //Linux Style
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability("marionette", true);
    
  • 将JDK更新为最新版本JDK 8u161

  • Selenium-Java客户端升级到v3.10.0
  • 清理IDE中的项目空间
  • 运行CCleaner工具以清除所有OS系统杂务。
  • 如果您的基本 Web浏览器版本太旧,请通过Revo Uninstaller将其卸载并安装最新的GA和发布的 Web浏览器版本。< / LI>
  • 进行系统重启
  • 执行 @Test

更新

当您使用 Selenium-Java Client 3.11.0 时,您必须使用FirefoxDriver类的构造函数列表中不支持 DesiredCapabilities 方法merge(Capabilities extraCapabilities)并将功能合并到FirefoxOptions Class对象中,如下所示:

package demo;

import java.util.logging.Level;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class A_Firefox_DC_Opt 
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver", "C:/Utility/BrowserDrivers/geckodriver.exe");
        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setCapability("marionatte", true);
        FirefoxOptions opt = new FirefoxOptions();
        opt.merge(dc);
        FirefoxDriver driver =  new FirefoxDriver(opt);
        driver.get("https://stackoverflow.com");
        System.out.println("Application opened");
        System.out.println("Page Title is : "+driver.getTitle());
        driver.quit();
    }
}

控制台输出:

1522037759633   geckodriver INFO    geckodriver 0.20.0
1522037759653   geckodriver INFO    Listening on 127.0.0.1:20073
1522037760415   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\rust_mozprofile.hRnaFvWiVBua"
1522037762202   Marionette  INFO    Enabled via --marionette
1522037765376   Marionette  INFO    Listening on port 1176
1522037765636   Marionette  WARN    TLS certificate errors will be ignored for this session
Mar 26, 2018 9:46:05 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Application opened
Page Title is : Stack Overflow - Where Developers Learn, Share, & Build Careers