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_USERSoftwareMicrosoftWindowsCurrentVersionInternet Settingsones
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/