使用IE11时无法切换到新窗口

时间:2017-01-19 15:09:34

标签: java html selenium-webdriver

我正在使用以下代码(适用于FireFox,显然前面部分不同)但在IE11中不起作用:

import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

@Before
public void setUp() throws Exception {
baseUrl = "not important";
DesiredCapabilities cap = new DesiredCapabilities();
DesiredCapabilities.internetExplorer().setCapability("ignoreProtectedModeSettings", true);
cap.setCapability("IE.binary", "C:/Program Files (x86)/Internet Explorer/iexplore.exe");
cap.setJavascriptEnabled(true);
cap.setCapability("requireWindowFocus", true);
cap.setCapability("enablePersistentHover", true);
File file = new File("C:\\Users\\pabraham\\Documents\\Selenium\\IEDriverServer_x64_2.53.1\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver.extractpath","C:\\Users\\pabraham\\Documents\\Selenium\\IEDriverServer_x64_2.53.1\\");
System.setProperty("webdriver.ie.logfile","C:\\Users\\pabraham\\Documents\\Selenium\\IEDriverServer_x64_2.53.1\\IEDriverServer.log");
System.setProperty("webdriver.ie.driver",file.getAbsolutePath());

driver = new InternetExplorerDriver(cap);

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
}

主要测试:

@Test
public void testExportedScript() throws Exception {
driver.get(baseUrl + "not important");

driver.findElement(By.id("overridelink")).click();
//TimeUnit.SECONDS.sleep(2); //This causes the password to fail???
String currentHandle = driver.getWindowHandle();

driver.findElement(By.id("j_username")).clear();
driver.findElement(By.id("j_username")).sendKeys("123");
driver.findElement(By.id("j_password")).clear();
driver.findElement(By.id("j_password")).sendKeys("123");
driver.findElement(By.id("j_id26")).click();

driver.findElement(By.id("workbenchPanel:j_id60:3:applications:1:j_id67")).findElement(By.linkText("UW")).click();
TimeUnit.SECONDS.sleep(5);

for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(currentHandle))
    {
        System.out.println("Try to select new window");
        TimeUnit.SECONDS.sleep(2);
        driver.switchTo().window(handle);
    }
}

String srnChk;
srnChk = driver.findElement(By.id("workbenchPanel:j_id60:3:applications:1:j_id67")).findElement(By.linkText("UW")).getText();
System.out.println("Check if its on the original screen: "+srnChk);
System.out.println("Try to click the override link");
driver.findElement(By.id("overridelink")).click();

TimeUnit.SECONDS.sleep(5);

我在最后添加了屏幕检查变量,以显示正在发生的事情。

我得到的错误是:org.openqa.selenium.NoSuchElementException:无法找到id == overridelink的元素

控制台显示代码没有点击代码的“尝试选择新窗口”部分:

启动InternetExplorerDriver服务器(64位) 2.53.1.0 在15536​​端口听 库提取到C:\ Users \ pabraham \ Documents \ Selenium \ IEDriverServer_x64_2.53.1 仅允许本地连接 检查它是否在原始屏幕上:UW 尝试单击覆盖链接

html(这是标准的网络安全证书屏幕):

<td id="continueToSiteAlign" align="left" valign="middle" aria-labelledby="continueToSite">
    <h4 id="continueToSite">
        <img src="red_shield.png" ID="ImgOverride" border="0" alt="Not recommended icon" class="actionIcon"><A href='' ID="overridelink" NAME="overridelink" >Continue to this website (not recommended).</A>
    </h4>
    </td>

出于好奇,当我在输入用户名和密码之前添加等待时,系统会说用户ID和密码不正确。当我等待时,它会毫无问题地登录。

1 个答案:

答案 0 :(得分:0)

我已经设法让这个工作了。我只需勾选互联网,本地互联网和可信站点的“启用保护模式”(互联网选项&gt;安全选项卡)。

它看起来也像'driver.findElement(By.id(&#34; j_password&#34;))。clear();'导致密码失败。我已经把它拿出来了,因此我没有遇到任何问题。我假设它是由计时问题引起的,因为这应该在输入密码之前运行。