如何使用Java关闭Selenium WebDriver中的子浏览器窗口
很长一段时间我在这里遇到了一些问题。 我无法弄清楚,有人愿意帮助我吗? …当我要完成新窗口的任务后,我要切换新窗口。 我想关闭那个新窗口。切换旧窗口,
所以这里我写的代码如下:
// 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]); }
// Perform the actions on new window
**`//Close the new window`** driver.close();
//perform remain operations in main window
//close entire webDriver session driver.quit();
//store instance of main window first using below code 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.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()或关闭窗口自身为空。
需要将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 }
另请注意,关闭最后一个窗口等同于quit()。
有两种方法可以关闭单个子窗口:
方式1:
driver.close();
方式2:使用键盘上的Ctrl + w键:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
public class First { public static void main(String[] args) { System.out.println("Welcome to Selenium"); WebDriver wd= new FirefoxDriver(); wd.manage().window().maximize(); wd.get("http://opensource.demo.orangehrmlive.com/"); wd.findElement(By.id("txtUsername")).sendKeys("Admin"); wd.findElement(By.id("txtPassword")).sendKeys("admin"); wd.findElement(By.id("btnLogin")).submit(); **wd.quit(); //--> this helps to close the web window automatically** System.out.println("Tested Sucessfully "); } }
我也试过了
1)driver.close();
2)driver.quit();
显然,这些方法不能按预期工作!(不是说它不起作用)我甚至尝试制作驱动程序类单例,它并没有帮助我运行并行测试用例。所以它也不是最佳解决方案。最后,我想创建一个单独的类来运行bat文件。bat文件包含对所有chrome驱动程序进程及其所有子进程进行分组的命令。以及我使用的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) { } } }
您必须在[.bat]文件中放入的命令
taskkill /IM "chromedriver.exe" /T /F