我试图在单击主窗口中的按钮后单击新打开的浏览器窗口中的按钮。每次显示一条消息:
Unable to find element on closed window
这是我正在使用的代码:
public Manage Administrators() {
String parentWindow = driver.getWindowHandle();
clickButton(waitUntilElementIsPresent(driver.findElement(By.xpath("//span[contains(text(),'Administrators')]"))));
Set<String> handles = driver.getWindowHandles();
for(String windowHandle : handles)
{
if(!windowHandle.equals(parentWindow))
{
driver.switchTo().window(windowHandle);
clickButton(Users);
driver.switchTo().window(parentWindow);
}
}
return PageFactory.initElements(driver, Manage.class);
似乎新窗口打开了,但 Selenium 不可见。
最佳答案
您必须等待新窗口打开。这可以通过如下方式完成:
String parentWindow = driver.getWindowHandle();
int prevWndCount = driver.getWindowHandles().size();
clickButton(waitUntilElementIsPresent(driver.findElement(By.xpath("//span[contains(text(),'Administrators')]"))));
// Waits for 60 seconds
WebDriverWait wait = new WebDriverWait(driver, 60);
// Wait until expected condition (Window count increases) met
wait.until((ExpectedCondition<Boolean>) new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
// Return true if window count increases, else return false
return d.getWindowHandles().size() > prevWndCount;
}
});
Set<String> handles = driver.getWindowHandles();
for(String windowHandle : handles)
{
if(!windowHandle.equals(parentWindow))
{
driver.switchTo().window(windowHandle);
clickButton(Users);
driver.switchTo().window(parentWindow);
}
}
return PageFactory.initElements(driver, Manage.class);
看看这是否有帮助!
关于java - 无法在窗口之间切换 - 无法显示浏览器消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25134280/