webDriver環境搭建與測試

來源:互聯網
上載者:User

標籤:

1、安裝jdk

2、安裝eclipse

3、安裝selenium

由於使用的是開發語言是java,因此需要安裝java版的selenium包。:http://pan.baidu.com/s/1hq6mebE,下載的版本為:selenium-java-2.53.0。下載完成後,進行解壓,得到如下目錄:

下載完jar包後,在已經建立的項目上右鍵,選擇properties,選擇java Build Path,選擇匯入額外的jar包,詳情如下:

匯入jar包後:

4、安裝瀏覽器驅動

Firefox--selenium原生支援,不需要安裝瀏覽器驅動

IE--需要安裝瀏覽器驅動

chrome--需要安裝瀏覽器驅動

以IE為例:

首先下載IEDriverServer.exe,將該驅動防止到IE瀏覽器安裝路徑下:C:\Program Files (x86)\Internet Explorer

配置系統路徑path,配置如下:

5、接下來編寫一個簡單的程式,測試環境搭建是否通過

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.ie.*;
public class WebDriverLearn {

        WebDriver driver = new InternetExplorerDriver();       
        driver.get("https://www.baidu.com/");        
    }
}

運行:

報錯如下:

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.ie.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver. The latest version can be downloaded from http://selenium-release.storage.googleapis.com/index.html
    at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
    at org.openqa.selenium.ie.InternetExplorerDriverService.access$0(InternetExplorerDriverService.java:1)
    at org.openqa.selenium.ie.InternetExplorerDriverService$Builder.findDefaultExecutable(InternetExplorerDriverService.java:167)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
    at org.openqa.selenium.ie.InternetExplorerDriver.setupService(InternetExplorerDriver.java:251)
    at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:172)
    at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:146)
    at WebDriverLearn.main(WebDriverLearn.java:27)

 

報錯原因:

找不到IE瀏覽器相關檔案,需要設定下system property。利用System.setProperty方法來添加路徑,代碼更新如下:

import org.openqa.selenium.*;
import org.openqa.selenium.ie.*;
public class WebDriverLearn {
    public static void main(String[] args) {
        System.setProperty("webdriver.ie.driver", "C:/Program Files (x86)/Internet Explorer/IEDriverServer.exe"); //指定IE瀏覽器安裝路徑
        WebDriver driver = new InternetExplorerDriver();   
        driver.get("https://www.baidu.com/");        
    }
}

運行本段代碼,能夠順利開啟IE瀏覽器。

注意:本文使用的selenium-java-2.53.0.jar,IE瀏覽器為IE9.

selenium和不同的瀏覽器,會有不同的相容性問題,需要選擇正確的相互相容的selenium和瀏覽器。目前只找到selenium-java-2.53.0.jar和IE9相容.

webDriver環境搭建與測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.