如何使用Selenium WebDriver在Java中检查弹出窗口是否打开

3
我正在尝试检查我想要打开的弹窗是否已经打开。
我已经查看了一些问题答案,例如:

如何使用selenium webdriver检查弹出窗口是否存在?

但是,没有什么帮助解决问题。
在这里,首先我通过点击登录按钮打开登录窗口。
driver.findElement(By.xpath("//a[@id='login_btn']")).click(); // Click Login Button

登录

我甚至尝试了getPageSource(),但好像不起作用。

任何形式的帮助都将不胜感激。

提前致谢。:)


可能它不是一个弹出窗口,只是一个框架。 - Kushal Bhalaik
2个回答

2
如果这是一个浏览器原生的弹窗(alert),您可以按照以下步骤进行操作:
try{
    driver.switchTo().alert();
    // If it reaches here, it found a popup
} catch(NoALertPresentException e){}

看起来你实际上正在处理一个iframe,获取“iframe”属性的值后,可以执行以下操作:
driver.switchTo.frame("ValueOfIframe");
// Treat as normal webpage. Now in iframe scope
driver.switchTo.defaultContent(); // To return back to normal page scope

1
String mwh=driver.getWindowHandle();

现在尝试通过执行某些操作打开弹出窗口:
driver.findElement(By.xpath("")).click();

Set s=driver.getWindowHandles(); //this method will gives you the handles of all opened windows

Iterator ite=s.iterator();

while(ite.hasNext())
{
    String popupHandle=ite.next().toString();
    if(!popupHandle.contains(mwh))
    {
        driver.switchTo().window(popupHandle);
        /**/here you can perform operation in pop-up window**
        //After finished your operation in pop-up just select the main window again
        driver.switchTo().window(mwh);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接