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

Unable to run selenium web driver project for 3.8.1 version

无法使用番石榴 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);

错误说明了一切:

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

Class NoSuchMethodError

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

解决方案

执行以下步骤:

  • 提供 绝对路径时 使用双反斜杠 (\) 或单正斜杠 (/)。两者是等价的。所以你需要更新 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.
  • 中清理 项目 Space
  • 运行 CCleaner 清除所有 OS 系统杂务的工具。
  • 如果您的基础 Web 浏览器 版本太旧,则通过 Revo Uninstaller 卸载它并安装 Web 的最新 GA 和发布版本浏览器.
  • 系统重启
  • 执行你的@Test

更新

当您使用 Selenium-Java 客户端 3.11.0 时,DesiredCapabilities 的支持已从构造函数列表中弃用FirefoxDriver Class you have to use the method merge(Capabilities extraCapabilities) and merge the capabilities into an 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://whosebug.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