如何使用 Java 在 Selenium WebDriver 中关闭子浏览器窗口
切换到新窗口并完成任务后,我想关闭那个新窗口并切换到旧窗口,
After I switch to a new window and complete the task, I want to close that new window and switch to the old window,
所以我在这里写了类似代码:
so here i written like code:
// Perform the click operation that opens new window
String winHandleBefore = driver.getWindowHandle();
// Switch to new window opened
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
driver.findElement(By.id("edit-name")).clear();
WebElement userName = driver.findElement(By.id("edit-name"));
userName.clear();
try
{
driver.quit();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("not close");
}
driver.switchTo().window(winHandleBefore);// Again I want to start code this old window
上面我写了代码driver.quit()
或driver.close()
.但我收到错误.谁能帮帮我...?
Above I written code driver.quit()
or driver.close()
. But I am getting error. Can anybody help me...?
org.openqa.selenium.remote.SessionNotFoundException: 调用 quit() 后无法使用 FirefoxDriver.
org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.
推荐答案
关闭单个浏览器窗口:
driver.close();
关闭所有(父+子)浏览器窗口并结束整个会话:
To close all (parent+child) browser windows and end the whole session:
driver.quit();
相关文章