【发布时间】:2014-03-03 08:45:46
【问题描述】:
我试图在弹出窗口出现时切换焦点,但 getWindowHandles().size() 只返回 1 个窗口。
如何确定我是否可以切换到弹出窗口?或弹出窗口在我的情况下不是新窗口
这是部分代码:
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
【问题讨论】:
-
它是
alert而不是弹出窗口? -
我试图做 driver.switchTo().alert() 但在错误堆栈跟踪中我有那个警报不存在。
-
既然你说窗口数保持为 1,我看不出切换窗口会有所帮助......你的弹出窗口可能是
iframe吗? -
不,它不是 iframe。我认为唯一的解决方案是插入 thread.sleep() (可能是不好的解决方案,但这是我唯一能做的)
-
我尽量使用
webdriverwaits,有时我也必须使用thread.sleep()。 “无论什么都行”有时是唯一的解决方案。