如何使用Java关闭SeleniumWebDriver中的子浏览器窗口
切换到新窗口并完成任务后,我想关闭新窗口并切换到旧窗口 因此,我在这里编写了类似于代码的代码:如何使用Java关闭SeleniumWebDriver中的子浏览器窗口,java,selenium,selenium-webdriver,webdriver,Java,Selenium,Selenium Webdriver,Webdriver,切换到新窗口并完成任务后,我想关闭新窗口并切换到旧窗口 因此,我在这里编写了类似于代码的代码: // Perform the click operation that opens new window String winHandleBefore = driver.getWindowHandle(); // Switch to new window opened for (String winHandle : driver.getWindowHandles()) {
// 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()
。但是我犯了一个错误。谁能帮我
org.openqa.selenium.remote.SessionNotFoundException:调用quit()后无法使用FirefoxDriver
要关闭单个浏览器窗口,请执行以下操作:
driver.close();
要关闭所有(父+子)浏览器窗口并结束整个会话,请执行以下操作:
driver.quit();
用于将控件切换到弹出窗口的逻辑是错误的
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
上述逻辑如何将控件切换到新窗口
使用以下逻辑将控件切换到新窗口
// get all the window handles before the popup window appears
Set beforePopup = driver.getWindowHandles();
// click the link which creates the popup window
driver.findElement(by).click();
// get all the window handles after the popup window appears
Set afterPopup = driver.getWindowHandles();
// remove all the handles from before the popup window appears
afterPopup.removeAll(beforePopup);
// there should be only one window handle left
if(afterPopup.size() == 1) {
driver.switchTo().window((String)afterPopup.toArray()[0]);
}
//Switch to new window opened
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
driver.close(); //this will close new opened window
//switch back to main window using this code
driver.switchTo().window(winHandleBefore);
// perform operation then close and quit
driver.close();
driver.quit();
//在新窗口上执行操作
**`//Close the new window`**
driver.close();
//close entire webDriver session
driver.quit();
//在主窗口中执行保留操作
**`//Close the new window`**
driver.close();
//close entire webDriver session
driver.quit();
执行打开新窗口的单击操作
// get all the window handles before the popup window appears
Set beforePopup = driver.getWindowHandles();
// click the link which creates the popup window
driver.findElement(by).click();
// get all the window handles after the popup window appears
Set afterPopup = driver.getWindowHandles();
// remove all the handles from before the popup window appears
afterPopup.removeAll(beforePopup);
// there should be only one window handle left
if(afterPopup.size() == 1) {
driver.switchTo().window((String)afterPopup.toArray()[0]);
}
//Switch to new window opened
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
driver.close(); //this will close new opened window
//switch back to main window using this code
driver.switchTo().window(winHandleBefore);
// perform operation then close and quit
driver.close();
driver.quit();
在某些情况下,从getWindowHandle()或getWindowHandles()获取有效的窗口句柄后,窗口将自动关闭 甚至有可能在getWindowHandles()运行时窗口会自动关闭,除非您创建一些关键的节类型代码(即在运行测试代码时冻结浏览器,直到所有窗口管理操作完成) 检查当前驱动程序有效性的一种更快的方法是检查sessionId,它由driver.close()或窗口关闭本身设置为null WebDriver需要强制转换到远程驱动程序接口(RemoteWebDriver)才能获得sessionId,如下所示:
if (null == ((RemoteWebDriver)driver).sessionId) {
// current window is closed, switch to another or quit
} else {
// current window is open, send commands or close
}
还要注意,关闭最后一个窗口相当于退出()。有两种方法可以关闭单个子窗口: 方式1:
driver.close();
方式2:使用键盘上的Ctrl+w键:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
我也试过了
1) driver.close()2) driver.quit() 显然,这些方法并没有像预期的那样有效!(并不是说它不起作用)我甚至尝试过使驱动程序类成为单例,但它并不能帮助我并行运行测试用例。因此,它也不是最佳解决方案。最后,我创建了一个单独的类来运行一个bat文件。bat文件包含对所有chrome驱动程序进程及其所有子进程进行分组的命令。我使用Runtime从java类执行了它 运行bat文件的类文件
public class KillChromeDrivers {
public static void main(String args[]) {
try {
Runtime.getRuntime().exec("cmd /c start E:\\Work_Folder\\SelTools\\KillDrivers.bat");
//Runtime.getRuntime().exec()
} catch (Exception ex) {
}
}
}
taskkill /IM "chromedriver.exe" /T /F
必须放入[.bat]文件的命令
public class KillChromeDrivers {
public static void main(String args[]) {
try {
Runtime.getRuntime().exec("cmd /c start E:\\Work_Folder\\SelTools\\KillDrivers.bat");
//Runtime.getRuntime().exec()
} catch (Exception ex) {
}
}
}
taskkill /IM "chromedriver.exe" /T /F
driver.close();适用于新窗口。driver.findElement(由.css选择器(“body”)).sendKeys(key.CONTROL+“w”);适用于新窗口和新选项卡。