今天安裝了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;
解決過程:
1、查了一下,據說報這個錯是因為在selenium和Firefox不兼容導致的,需要添加驅動。於是找到了驅動下載地址:
下載網址1:http://download.csdn.net/detail/just_h_mao/9670229
下載網址2: https://github.com/mozilla/geckodriver/releases/tag/v0.9.0
2、設置驅動路徑
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", "C:\\Program Files (x86)\\Mozilla Firefox\\webdriver\\geckodriver-v0.16.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 }
運行后發現打開瀏覽器就沒有后續了,檢查控制台還是報錯。
推測還是因為不兼容導致,所以干脆將selenium升級到最新,官網下載(需翻牆,推薦LOCO)。下載后重新加載JAR,解決!