【3. 測試 Chrome】
Chrome 雖然不是 Selenium 的原配,但是沒辦法,她太火辣了,不能拋下她不管的。
把 ExampleForFireFox.java 稍微修改可以制作出一個(gè) ExampleForChrome.java ,直接把 new FireFoxDriver() 修改為 new ChromeDriver() 你會(huì)發(fā)現(xiàn)還是行不通。
錯(cuò)誤如下:
1)Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
這應(yīng)該是找不到 chrome 的文件,好吧,利用 System.setProperty 方法添加路徑,這里要注意,是 “webdriver.chrome.driver” 可不是“webdriver.chrome.bin”
設(shè)置路徑后還是會(huì)報(bào)錯(cuò):
2)[6416:4580:1204/173852:ERROR:gpu_info_collector_win.cc(91)] Can't retrieve a valid WinSAT assessment.
這個(gè)貌似是因?yàn)?Selenium 無法直接啟動(dòng) Chrome 導(dǎo)致的,必須要通過前面咱們下載 Chrome 的第三方插件 ChromeDriver,去看第一個(gè)錯(cuò)誤中提示給你的 網(wǎng)址:http://code.google.com/p/selenium/wiki/ChromeDriver
按照人家給的例子來修改我們的測試代碼吧:
01 packagelesson1;
02
03 importjava.io.File;
04 importjava.io.IOException;
05
06 importorg.openqa.selenium.By;
07 importorg.openqa.selenium.WebDriver;
08 importorg.openqa.selenium.WebElement;
09 importorg.openqa.selenium.chrome.ChromeDriverService;
10 importorg.openqa.selenium.remote.DesiredCapabilities;
11 importorg.openqa.selenium.remote.RemoteWebDriver;
12 importorg.openqa.selenium.support.ui.ExpectedCondition;
13 importorg.openqa.selenium.support.ui.WebDriverWait;
14
15 publicclassExampleForChrome {
16 publicstaticvoidmain(String[] args)throwsIOException {
17 // 設(shè)置 chrome 的路徑
18 System.setProperty(
19 "webdriver.chrome.driver",
20 "C:\Documents and Settings\sq\Local Settings\Application Data\Google\Chrome\Application\chrome.exe");
21 // 創(chuàng)建一個(gè) ChromeDriver 的接口,用于連接 Chrome
22 @SuppressWarnings("deprecation")
23 ChromeDriverService service =newChromeDriverService.Builder()
24 .usingChromeDriverExecutable(
25 newFile(
26 "E:\Selenium WebDriver\chromedriver_win_23.0.1240.0\chromedriver.exe"))
27 .usingAnyFreePort().build();
28 service.start();
29 // 創(chuàng)建一個(gè) Chrome 的瀏覽器實(shí)例
30 WebDriver driver =newRemoteWebDriver(service.getUrl(),
31 DesiredCapabilities.chrome());
32
33 // 讓瀏覽器訪問 Baidu
34 driver.get("http://www.baidu.com");
35 // 用下面代碼也可以實(shí)現(xiàn)
36 // driver.navigate().to("http://www.baidu.com");
37
38 // 獲取 網(wǎng)頁的 title
39 System.out.println("1 Page title is: "+ driver.getTitle());
40
41 // 通過 id 找到 input 的 DOM
42 WebElement element = driver.findElement(By.id("kw"));
43
44 // 輸入關(guān)鍵字
45 element.sendKeys("zTree");
46
47 // 提交 input 所在的 form
48 element.submit();
49
50 // 通過判斷 title 內(nèi)容等待搜索頁面加載完畢,間隔10秒
51 (newWebDriverWait(driver,10)).until(newExpectedCondition<Boolean>() {
52 publicBoolean apply(WebDriver d) {
53 returnd.getTitle().toLowerCase().endsWith("ztree");
54 }
55 });
56
57 // 顯示搜索結(jié)果頁面的 title
58 System.out.println("2 Page title is: "+ driver.getTitle());
59
60 // 關(guān)閉瀏覽器
61 driver.quit();
62 // 關(guān)閉 ChromeDriver 接口
63 service.stop();
64
65 }
66 }
運(yùn)行一下看看,是不是一切OK了?
補(bǔ)充:仔細(xì)看了一下官網(wǎng)的介 紹:Chrome Driver is maintained / supported by the Chromium project iteslf. 看來如果使用 new ChromeDriver() 的話,應(yīng)該要安裝 Chromium 而不是 Chrome,我現(xiàn)在懶得折騰了,有興趣的童鞋可以試驗(yàn)一下。