今天安装了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升级到最新,官网下载(需FQ,推荐LOCO)。下载后重新加载JAR,解决!