Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium webdriver-使用getWindowHandles()选择弹出窗口_Selenium_Window Handles - Fatal编程技术网

Selenium webdriver-使用getWindowHandles()选择弹出窗口

Selenium webdriver-使用getWindowHandles()选择弹出窗口,selenium,window-handles,Selenium,Window Handles,我试图在弹出窗口出现时切换焦点,但getWindowHandles().size()只返回一个窗口 如何确定我是否可以切换到弹出窗口?或者弹出窗口不是我的新窗口 以下是代码的一部分: String parentWindowHandlerP = driver.getWindowHandle(); // save ID parent window String subWindowHandlerP = null; // action to call popup (new WebDri

我试图在弹出窗口出现时切换焦点,但getWindowHandles().size()只返回一个窗口

如何确定我是否可以切换到弹出窗口?或者弹出窗口不是我的新窗口

以下是代码的一部分:

String parentWindowHandlerP = driver.getWindowHandle(); // save ID parent window
String subWindowHandlerP = null;

    // action to call popup
    (new WebDriverWait(driver, 5)).until(ExpectedConditions.elementToBeClickable(By.id("pt1:pt_region0:2:pt1:ilov1::lovIconId"))).click();

    Set<String> handlesP = driver.getWindowHandles(); // get all windows
    Iterator<String> iteratorP = handlesP.iterator();
    while (iteratorP.hasNext()) {
        subWindowHandlerP = iteratorP.next();
    }
    // popup operations     
    driver.findElement(By.id("pt1:pt_region0:2:pt1:t1:_afrFltrc6::content")).sendKeys(Keys.RETURN);
    driver.findElement(By.id("pt1:pt_region0:2:pt1:ilov1_afrLovDialogId::ok")).click();

    driver.switchTo().window(parentWindowHandlerP); // back na parent window
String parentWindowHandlerP=driver.getWindowHandle();//保存ID父窗口
字符串subWindowHandlerP=null;
//调用弹出窗口的操作
(新的WebDriverWait(驱动程序,5))。直到(ExpectedConditions.ElementToBickable(By.id(“pt1:pt_region0:2:pt1:ilov1::lovIconId”))。单击();
Set handlesP=driver.getWindowHandles();//获取所有窗口
迭代器iteratorP=handlesP.Iterator();
while(iteratorP.hasNext()){
subWindowHandlerP=iteratorP.next();
}
//弹出操作
driver.findElement(By.id(“pt1:pt_region0:2:pt1:t1:_afrFltrc6::content”)).sendKeys(key.RETURN);
driver.findElement(By.id(“pt1:pt_region0:2:pt1:ilov1_afrLovDialogId::ok”))。单击();
driver.switchTo().window(parentWindowHandlerP);//返回父窗口

您可以使用以下内容:

    String parentWindowHandlerP = driver.getWindowHandle(); 
    // action to call popup
    (new WebDriverWait(driver, 5)).until(ExpectedConditions.elementToBeClickable(By.id("pt1:pt_region0:2:pt1:ilov1::lovIconId"))).click();

    while (driver.getWindowHandles().size() < 2) {
        Thread.sleep(500);
    }
    Set<String> handles = driver.getWindowHandles();
    for (String windowHandle : handles) {
        if (!windowHandle.equals(parentWindowHandlerP)) {
            driver.switchTo().window(windowHandle);
            break;
        }
    }
String parentWindowHandlerP=driver.getWindowHandle();
//调用弹出窗口的操作
(新的WebDriverWait(驱动程序,5))。直到(ExpectedConditions.ElementToBickable(By.id(“pt1:pt_region0:2:pt1:ilov1::lovIconId”))。单击();
while(driver.getWindowHandles().size()<2){
睡眠(500);
}
Set handles=driver.getWindowHandles();
用于(字符串windowHandle:handles){
如果(!windowHandle.equals(parentWindowHandlerP)){
driver.switchTo().window(windowHandle);
打破
}
}

它是一个
警报
而不是一个弹出窗口吗?我试图执行driver.switch to().alert(),但在错误中,stacktrace显示该警报不存在。因为你说的是窗口数保持为1,所以我看不出切换窗口是否有帮助。。。您的弹出窗口可能是
iframe
吗?不,不是iframe。我认为唯一的解决办法是插入thread.sleep()(可能是糟糕的解决方案,但这是我唯一能做的事情)我尽量使用
webdriverwaits
,有时我也不得不使用
thread.sleep()
。“一切正常”有时是唯一的解决方案。在我使用此部分od代码的情况下,如果(!windowHandle.equals(parentWindowHandlerP))无法传递此代码,因为parentWindow和popupWindow具有相同的ID:(