Selenium:无法切换窗口

时间:2013-10-23 13:11:52

标签: java testing selenium

我正在尝试切换到另一个弹出窗口,但是会出现某种错误。我无法弄清楚为什么代码不能用于切换窗口

它给出的错误是

java.lang.NullPointerException:条目中的null值:name = null

这是代码......

String parentWindowHandle = driver.getWindowHandle();
        System.out.println("parentWindowHandle......"+parentWindowHandle);
        //click on "register"

        Actions act = new Actions(driver);
            act.click(driver.findElement(By.id("ctl00_ContentPlaceHolder1_btnRegisterMe")));
        act.perform();

        sleep(26000); 
        String popUpWindowHandle=null;
        Set<String> openWindowsList = driver.getWindowHandles();
        for (String windowHandle : openWindowsList) {
             System.out.println("windowHandle......"+windowHandle);
            if (!windowHandle.equals(parentWindowHandle))
                {popUpWindowHandle = windowHandle;

              break;}
        }                                                
                // Switching control to newly opened window

        driver.switchTo().window(popUpWindowHandle);

1 个答案:

答案 0 :(得分:0)

如果您能够打印窗口句柄,请尝试

if (!windowHandle.equalsIgnoreCase(parentWindowHandle)){
            popUpWindowHandle = windowHandle;
            break;
}

而不是

if(!windowHandle.equals(parentWindowHandle)){}