1. 程式人生 > >The path to the driver executable must be set by the webdriver.gecko.driver system property;

The path to the driver executable must be set by the webdriver.gecko.driver system property;

pack 如果 leg ide rec sel window win find

今天安裝了webdriver後,編寫了一個測試小程序,但運行時控制臺報了這樣的錯誤:

 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 
https://github.com/mozilla/geckodriver/releases

解決過程:

1、查了一下,據說報這個錯是因為在selenium3中,使用Firefox需要添加驅動。於是從晚上找驅動下載地址:

下載網址1:http://download.csdn.net/detail/just_h_mao/9670229

下載網址2: https://github.com/mozilla/geckodriver/releases/tag/v0.9.0

2、

註意:如果firefox安裝時使用了非默認安裝路徑,則可以無法找到firefox.exe來啟動執行此腳本的firefox瀏覽器。在new FirefoxDriver()前設置一下瀏覽器安裝路徑即可解決,如下:

 1 package helloworld;
 2 import org.openqa.selenium.*;
 3 import org.openqa.selenium.By.ById;
 4 import org.openqa.selenium.firefox.FirefoxDriver;
 5 
 6 public class JavaTest {
 7 
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         
11         WebDriver driver;
12 String baseurl; 13 System.setProperty("webdriver.gecko.driver", "F:\\WebDriver\\下載\\geckodriver-v0.11.1-win64\\geckodriver.exe");
14 driver=new FirefoxDriver(); 15 baseurl="http://www.sogou.com/"; 16 //打開搜狗首頁 17 driver.get(baseurl+"/"); 18 //在搜索框輸入XX 19 driver.findElement(By.id("query")).sendKeys("XX"); 20 //單擊搜索按鈕 21 driver.findElement(By.id("stb")).click(); 22 23 24 } 25 26 }

但運行後還是出現錯誤:

技術分享
1495555671697    geckodriver    INFO    Listening on 127.0.0.1:30859
五月 24, 2017 12:07:52 上午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Attempting bi-dialect session, assuming Postel‘s Law holds true on the remote end
1495555672392    mozprofile::profile    INFO    Using profile path C:\Users\admn\AppData\Local\Temp\rust_mozprofile.62d2gzZYduHH
1495555672416    geckodriver::marionette    INFO    Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe
1495555672563    geckodriver::marionette    INFO    Connecting to Marionette on localhost:18028
1495555677486    Marionette    INFO    Listening on port 18028
五月 24, 2017 12:07:57 上午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: Expected [object Undefined] undefined to be a string
Build info: version: ‘unknown‘, revision: ‘1969d75‘, time: ‘2016-10-18 09:43:45 -0700‘
System info: host: ‘admin‘, ip: ‘192.168.1.102‘, os.name: ‘Windows 8.1‘, os.arch: ‘amd64‘, os.version: ‘6.3‘, java.version: ‘1.8.0_31‘
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\admn\AppData\Local\Temp\rust_mozprofile.62d2gzZYduHH, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0.2, platformVersion=10.0, moz:processID=14196, browserName=firefox, platformName=windows_nt}]
Session ID: 7a0a1695-fe51-4d87-a720-2f7c1baa7038
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:274)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:98)
    at helloworld.JavaTest.main(JavaTest.java:19)
View Code

The path to the driver executable must be set by the webdriver.gecko.driver system property;