selenium-java自動化測試環境搭建中的問題以及解決方法
執行如下程式碼,報錯:
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.bin","C://Program Files//Mozilla Firefox//firefox.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.baidu.com");
}
}
問題:
1、Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from
at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
at org.openqa.selenium.remote.se。。。
解決:
火狐瀏覽器版本太高,要麼更換低版本的firefox,要麼加上firefox驅動。
驅動下載地址:
https://github.com/mozilla/geckodriver/releases
加上程式碼:
System.setProperty("webdriver.gecko.driver","E:\\selenium\\geckodriver-v0.23.0-win64\\geckodriver.exe");
(後面是驅動的檔案位置)
(
在這之前還有匯入jar包版本太低的問題,在maven中重新配置更新即可:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.14.0</version>
</dependency>
)