Selenium webdriver Java 操作IE瀏覽器


V1.0版本:直接新建WebDriver使用

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class IETest {
    public static void main(String[] args) {
WebDriver wd = new InternetExplorerDriver(); wd.get("http://www.baidu.com"); try { Thread.sleep(1200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(wd.getCurrentUrl()); wd.quit(); } }

 結果:運行出錯

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 http://code.google.com/p/selenium/wiki/InternetExplorerDriver. The latest version can be downloaded from http://selenium-release.storage.googleapis.com/index.html

分析:selenium找不到IE Driver。

V2.0版本:使用IEDriverServer

Step1: 下載IEDriverServer。

下載路徑:http://selenium-release.storage.googleapis.com/index.html

我下載的是2.48版本的IEDriverServer_Win32_2.48.0.zip ,解壓之后得到IEDriverServer.exe 。打開2.48,可以看到兩個IEDriverServer:

32bit:  IEDriverServer_Win32_2.48.0.zip

64bit:  IEDriverServer_x64_2.48.0.zip

選擇一個合適的下載即可。

 

Step2: 放置IEDriverServer

在跟項目包平行的地方新建一個包,比如”lib",然后將 IEDriverServer.exe拷貝到lib下。

 

Step3: 添加 webdriver.ie.driver 屬性設置

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class IETest {

    public static void main(String[] args) {
        System.setProperty("webdriver.ie.driver","src/lib/IEDriverServer.exe");
        WebDriver wd = new InternetExplorerDriver();
        wd.get("http://www.baidu.com");
        try {
            Thread.sleep(1200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(wd.getCurrentUrl());
        wd.quit();

    }
}

結果:運行出錯

Started InternetExplorerDriver server (32-bit)
2.48.0.0
Listening on port 38600
Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

分析:微軟自IE7后加入了Protected Mode(保護模式)

V3.0版本:更改保護模式

Step1: Win+R打開“運行”,輸入“regedit.exe",打開注冊表

 

Step2: 找到HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones

 

Step3: 可以看到5個文件夾,分別為: 0 1 2 3 4 。

在1-4號Key下面都有名叫2500的屬性,則將其值改為相同的非零值。一般情況下,系統上默認的是1,2號key 2500屬性值為3,3,4號key 2500屬性為0,將3,4號key的值該成3就可以。

 

Step4: 點擊“應用”,“確定”,使更改生效。

 

Step5: 重新運行上面的程序。

結果: 運行通過

Started InternetExplorerDriver server (32-bit)
2.48.0.0
Listening on port 41898
https://www.baidu.com/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM