我正在使用硒自动化技术。我已经编码为登录页面,并且可以正常工作。
通过登录并因此打开了新的子窗口,并且关闭了父窗口。
因此,我的Web驱动程序停止运行并导致异常。
线程异常main
org.openqa.selenium.NoSuchWindowException
:
找不到窗口(警告:服务器未提供任何堆栈跟踪信息)
请帮忙
Selenium保留一个窗口(句柄)列表。网络驱动程序需要指向正确的句柄。关闭窗口后,其句柄将被删除,您的驱动程序现在将指向不再存在的对象。
可能,您必须显式切换窗口以将驱动程序指向正确的窗口句柄。也许这可以帮助:
“结果打开了新的子窗口,并且关闭了父窗口”
下面的(1)中的代码向您展示如何检索窗口句柄的列表,以及如何基于其标题检索正确的句柄。
private void handleMultipleWindows(String windowTitle) {
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
if (driver.getTitle().contains(windowTitle)) {
return;
}
}
}
您可以使用该功能(或类似功能)来检索新窗口。从您提供的代码中,将得出:
// Entering the credentials in the login window.
driver.findElement(By.id(txtUserId)).clear();
driver.findElement(By.id(txtUserId)).sendKeys(poovan);
driver.findElement(By.id(txtPassword)).clear();
driver.findElement(By.id(txtPassword)).sendKeys(welcome1);
driver.findElement(By.id(btnSubmit)).click();
// Here the login window gets closed, handler to that window disappears, and driver becomes stale.
// So we need update the driver to point to the new window
handleMultipleWindows("The title of my new window");
driver.findElement(By.name(bono)).sendKeys(080);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句