【使用selenium打开IE浏览器步骤】:
1、在IE浏览器上运行测试脚本,首先需要下载IEDriverServer.exe,放在IE浏览器的安装目录且同级目录下.
2、参考代码如下:
import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait;import com.thoughtworks.selenium.webdriven.commands.WaitForCondition; public class SeleniumTest{ private WebDriver driver; @Before public void setUp(){ System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Internet Explorer\\IEDriverServer.exe"); driver = new InternetExplorerDriver(); System.out.println("打开浏览器"); } @Test public void testLogic(){ System.out.println("打开——>百度一下"); driver.get("http://www.baidu.com/"); WebDriverWait wait = new WebDriverWait(driver, 10); WebElement kw = wait.until(new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("kw")); } }); try { if(kw!=null){ kw.sendKeys("selenium"); driver.findElement(By.id("su")).click(); Thread.sleep(1000); } System.out.println(driver.getCurrentUrl()); Thread.sleep(10000000); } catch (InterruptedException e) { e.printStackTrace(); } } @After public void tearDown(){ if(driver!=null){ driver.quit(); } } }
【遇到的问题及其解决方案】:
1、报错:
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
解决方法:
设置 system property:System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Internet Explorer\\IEDriverServer.exe");
2、报错:
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. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 1.15 seconds
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'PC-201wegfer', ip: '10.1.9.173', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_43'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
解决办法:
IE安全保护都去掉:
internet选项——安全
internet-启用保护模式 勾去掉
本地internet-启用保护模式 勾去掉
可信站点-启用保护模式 勾去掉
除了上面的那几个,还需要在“受限制站点” 去除启用保护模式
只有当知道了全部,你的选择才有意义。