按照网上的例子会遇到“
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是驱动放置的位置
-
package seleniumtest.seleniumtest;
-
-
import org.openqa.selenium.By;
-
import org.openqa.selenium.WebDriver;
-
import org.openqa.selenium.WebElement;
-
import org.openqa.selenium.firefox.FirefoxDriver;
-
import org.openqa.selenium.firefox.FirefoxBinary;
-
import org.openqa.selenium.chrome.*;
-
-
/**
-
* test Baidu By Id
-
*
-
*/
-
public class testBaiduById
-
{
-
public static void main( String[] args )
-
{
-
//System.setProperty("webdriver.firefox.marionette","C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
-
System.setProperty("webdriver.gecko.driver","C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
-
-
WebDriver driver = new FirefoxDriver();
-
-
//System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
-
//WebDriver driver = new ChromeDriver();
-
-
driver.get("");
-
-
WebElement searchBox = driver.findElement(By.id("kw"));
-
searchBox.sendKeys("test Baidu by Id");
-
-
WebElement searchButton = driver.findElement(By.id("su"));
-
searchButton.submit();
-
-
driver.close();
-
}
-
}
阅读(1473) | 评论(0) | 转发(0) |