http://blog.csdn.net/jinhe123/article/details/69946234
Selenium作為網(wǎng)站UI測試?yán)饕磷蹋瑸楹诤泄δ軠y試人員所喜愛唯欣。
下載Selenium的最新版本地址:http://selenium-release.storage.googleapis.com/index.html
友情提示:如果一直下載不了,可能是瀏覽器與下載工具的沒有兼容序愚,或者沒安裝下載工具的插件。用IE瀏覽器打開勺三,可以完整下載愕鼓。如果沒有這個問題就忽略钙态。
時至今日,Selenium已經(jīng)到了3.3.1版了(2017年3月7日)菇晃。
自從Selenium3發(fā)布以來册倒,火狐瀏覽器(Selenium支持火狐的技術(shù)最為成熟,因為可以方便獲取從而控制網(wǎng)頁信息磺送,也是測試人員最喜歡用的瀏覽器之一)成為了一個普遍的問題驻子。
因為Selenium3不支持向前支持火狐瀏覽器了,40以后版本的火狐估灿,運行會出現(xiàn)問題崇呵。
例如Java代碼:(實現(xiàn)打開瀏覽器,輸入"WebDriver"馅袁,搜索后域慷,關(guān)閉瀏覽器)
[java]?view plain?copy
package?com.selenium.test;??
import?org.openqa.selenium.By;??
import?org.openqa.selenium.WebDriver;??
import?org.openqa.selenium.WebElement;??
import?org.openqa.selenium.firefox.FirefoxDriver;??
public?class?seleniumHello?{??
public?static?void?main(String[]?args)?{??
//如果火狐瀏覽器沒有默認安裝在C盤,需要自己確定其路徑??
System.setProperty("webdriver.firefox.bin",?"D:/Program?Files/Mozilla?Firefox/firefox.exe");???
//定義驅(qū)動對象為?FirefoxDriver?對象??
WebDriver?driver?=new?FirefoxDriver();??
//打開的網(wǎng)址??
driver.get("http://www.baidu.com/");??
//定位輸入框元素??
WebElement?txtbox?=?driver.findElement(By.name("wd"));??
//在輸入框輸入文本??
txtbox.sendKeys("WebDriver");??
//定位按鈕元素??
WebElement?btn?=?driver.findElement(By.id("su"));??
//點擊按鈕??
????????btn.click();??
//關(guān)閉驅(qū)動??
????????driver.close();??
????}??
}??
友情提示:如果運行后汗销,你發(fā)現(xiàn)只打開了瀏覽器犹褒,而沒有打開網(wǎng)址。那么多數(shù)是版本問題:Selenium版本為3大溜,或者火狐版本太高化漆,一般Selenium2支持46以下的。
目前火狐版本是:50
現(xiàn)在運行會發(fā)現(xiàn)問題钦奋,在console中出現(xiàn)的提示是:
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
這就是要求要安裝geckodriver了(支持3.3.1)座云。
之前版本的Selenium客戶端疙赠,遵循:W3C WebDriver specification? 鏈接:https://w3c.github.io/webdriver/webdriver-spec.html
解決的辦法就是:
1、到官網(wǎng)上下載與系統(tǒng)相應(yīng)的最新版本geckodriver:https://github.com/mozilla/geckodriver/releases
從2015年4月9日的wires V0.1.0版朦拖,直到2017年3月8日的最新V0.15.0圃阳,走過了15次的版本更新。
2璧帝、解壓后捍岳,將文件存放在自己設(shè)置的一個目錄里。
例如睬隶,我下載的是windows版本锣夹,將其放在D:\selenium目錄下,文件名為:geckodriver.exe
3苏潜、將代碼改為如下:
[java]?view plain?copy
package?com.selenium.test;??
import?org.openqa.selenium.By;??
import?org.openqa.selenium.WebDriver;??
import?org.openqa.selenium.WebElement;??
import?org.openqa.selenium.firefox.FirefoxDriver;??
public?class?seleniumHello?{??
public?static?void?main(String[]?args){??
System.setProperty("webdriver.gecko.driver",?"D:\\selenium\\geckodriver.exe");??
WebDriver?driver=new?FirefoxDriver();??
driver.get("http://www.baidu.com/");??
????????driver.manage().window().maximize();??
WebElement?txtbox=driver.findElement(By.name("wd"));??
txtbox.sendKeys("WebDriver");??
WebElement?btn=driver.findElement(By.id("su"));??
????????btn.click();??
????????driver.close();??
????}??
}??
執(zhí)行后银萍,在控制臺顯示:
瀏覽器正常啟動,并執(zhí)行了搜索恤左。
什么是Gecko和Gecko Driver
Gecko是由Mocilla開發(fā)的許多應(yīng)用程序中的Web瀏覽器引擎贴唇。
Gecko則是一種介于你的Selenium的測試代碼與Firfox瀏覽器之間的鏈接,是使用W3C?
WebDriver兼容客戶端的一種代理與Gecko核心瀏覽器交互飞袋。
火狐瀏覽器用可執(zhí)行程序GeckoDriver.exe的方式執(zhí)行WebDriver協(xié)議戳气。所有的測試腳本都通過GeckoDriver來執(zhí)行。
其他瀏覽器:Chrome
平時用的四款瀏覽器:Chrome巧鸭、360瓶您、IE和火狐。
平時使用的話從穩(wěn)定蹄皱、速度實用的角度览闰,推薦Chrome,打開谷歌也更穩(wěn)定和順暢巷折。
這里也順便提一下压鉴,Selenium webDriver 如何連上Chrome
Chrome瀏覽器要下載一個ChromeDriver.exe
下載地址:https://sites.google.com/a/chromium.org/chromedriver/,目前最新版本是2.29
代碼寫為如下:
[java]?view plain?copy
package?com.selenium.test;??
import?org.openqa.selenium.By;??
import?org.openqa.selenium.WebDriver;??
import?org.openqa.selenium.WebElement;??
import?org.openqa.selenium.chrome.ChromeDriver;??
public?class?seleniumHello?{??
public?static?void?main(String[]?args){??
System.setProperty("webdriver.chrome.driver",?"D:\\selenium\\ChromeDriver.exe");??
WebDriver?driver=new?ChromeDriver();??
driver.get("http://www.baidu.com/");??
????????driver.manage().window().maximize();??
WebElement?txtbox=driver.findElement(By.name("wd"));??
txtbox.sendKeys("WebDriver");??
WebElement?btn=driver.findElement(By.id("su"));??
????????btn.click();??
try?{??
Thread.sleep(5000);??
}catch?(InterruptedException?e)?{??
????????????e.printStackTrace();??
????????}??
????????driver.close();??
????}??
}??
就可以正常打開Chrome并進行搜索了锻拘。