如果您使用的是Firefox版本48或更高版本,您必須先下載木偶司機:
https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver
選擇適合你的系統版本(在Windows/Linux操作系統,32位或64位),下載它並更新Path系統變量以將完整的目錄路徑添加到可執行文件。
見changelog的官方信息:https://github.com/SeleniumHQ/selenium/blob/master/dotnet/CHANGELOG
Geckodriver現在是自動化的Firefox的默認機制。 這是Mozilla的實現驅動程序的該瀏覽器, 和自動化所需的Firefox版本48及以上。
我不知道你怎麼使用eclipse下載硒。你從他們的頁面下載庫(罐),並手動將它們作爲使用Java構建路徑/庫選項在Eclipse外部罐子?
總之,在我看來,最簡單的方法是將項目轉化爲Maven項目:
的pom.xml
在我的示例項目的全部內容:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>WebKierowca</groupId>
<artifactId>WebKierowca</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
</project>
最後創建下面的Java類,變化指着木偶驅動程序(geckodriver.exe)的路徑,右擊這個類並運行它爲Java應用程序。如果一切正常,就應該啓動Firefox,去谷歌網頁,搜索詞「硒」,並顯示搜索結果,持續5秒:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
public static void main(String ... x){
// Path to Marionette driver
System.setProperty("webdriver.gecko.driver", "C:/serwery/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Selenium");
driver.findElement(By.name("btnG")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.quit();
}
}
謝謝!有效! –