如何使用Java关闭Selenium WebDriver中的子浏览器窗口 [英] How to close child browser window in Selenium WebDriver using Java
问题描述
这里我很长一段时间遇到了一些问题。我无法弄清楚,有人愿意帮助我吗? ...当我要完成新窗口的任务后,我要切换新窗口。我想关闭那个新窗口。并切换旧窗口,
Here I am getting some problem since a long time. And I couldn't figure out that,can anybody would like to help me? ... when i am going to switch new window ,after complete the task of new windows. I want to close that new window.and switch 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();
这篇关于如何使用Java关闭Selenium WebDriver中的子浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!