參考地址:http://www.cnblogs.com/fnng/p/5932224.html
https://github.com/mozilla/geckodriver
【火狐瀏覽器】
火狐瀏覽器的驅動下載地址:https://github.com/mozilla/geckodriver/releases
要求火狐瀏覽器版本:Support is best in Firefox 52.0.3 and onwards,最好是52及之后的版本
要求selenium版本:Selenium3.0及+
新建一個文件夾,如d:\seleniumdriver,將geckodriver.exe放置到該文件夾,再將d:\seleniumdriver配置到系統變量的Path里面
package ant_junit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class TestFirefox { public static void main(String[] args) {
//如果火狐不是在默認目錄下,需要這樣設置一下火狐的路徑,不然會報找不到火狐的錯誤 System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); WebDriver dr = new FirefoxDriver(); dr.get("http://www.baidu.com"); dr.findElement(By.id("kw")).sendKeys("test"); dr.quit(); } }
【谷歌瀏覽器】
谷歌瀏覽器的驅動下載地址:https://code.google.com/p/chromedriver/downloads/list,但是國內有時訪問不了,可以去網上其他地址搜索下一個
要求谷歌瀏覽器版本:一般最新的都支持
新建一個文件夾,如d:\seleniumdriver,將chromedriver.exe放置到該文件夾,再將d:\seleniumdriver配置到系統變量的Path里面
package ant_junit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class TestChromeDriver { public static void main(String[] args) {
//一般谷歌的安裝都在默認目錄下,當將chromedriver.exe配置到系統Path后,則可直接啟動 WebDriver dr = new ChromeDriver(); dr.get("http://www.baidu.com"); dr.findElement(By.id("kw")).sendKeys("test"); dr.quit(); } }
備注:
本人機器上測試成功的JDK版本:1.8
Selenium服務器版本:selenium-server-standalone-3.4.0.jar