Chinaunix首页 | 论坛 | 博客
  • 博客访问: 399182
  • 博文数量: 119
  • 博客积分: 1470
  • 博客等级: 上尉
  • 技术积分: 1258
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-24 13:50
文章分类

全部博文(119)

文章存档

2018年(6)

2017年(11)

2016年(4)

2013年(8)

2012年(1)

2011年(2)

2010年(4)

2009年(37)

2008年(16)

2006年(30)

我的朋友

分类: Java

2017-06-19 10:16:31

按照网上的例子会遇到“The path to the driver executable must be set by the webdriver.gecko.driver system property” 这个错误, 下面是解决办法:

报这个错,是因为你使用了selenium3+Firefox。在selenium3中,使用Firefox,需要添加驱动。

您可以从Github上下载驱动程序下载网址-  

在代码中加入

System.setProperty("webdriver.gecko.driver","C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();

C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe是驱动放置的位置

  1. package seleniumtest.seleniumtest;

  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.WebElement;
  5. import org.openqa.selenium.firefox.FirefoxDriver;
  6. import org.openqa.selenium.firefox.FirefoxBinary;
  7. import org.openqa.selenium.chrome.*;

  8. /**
  9.  * test Baidu By Id
  10.  *
  11.  */
  12. public class testBaiduById
  13. {
  14.     public static void main( String[] args )
  15.     {
  16.         //System.setProperty("webdriver.firefox.marionette","C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
  17.         System.setProperty("webdriver.gecko.driver","C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");

  18.         WebDriver driver = new FirefoxDriver();
  19.         
  20.         //System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
  21.         //WebDriver driver = new ChromeDriver();
  22.         
  23.         driver.get("");
  24.         
  25.         WebElement searchBox = driver.findElement(By.id("kw"));
  26.         searchBox.sendKeys("test Baidu by Id");
  27.         
  28.         WebElement searchButton = driver.findElement(By.id("su"));
  29.         searchButton.submit();
  30.         
  31.         driver.close();
  32.     }
  33. }



阅读(1438) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~