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
Above I written code driver.quit()
or driver.close()
. But I am getting error. Can anybody help me...?
org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.
A child window opens as separate window as a tab. Go back to the parent and click “Close child” and the child window closes. Viola!
We can close the child browser window in Selenium webdriver. The getWindowHandles and getWindowHandle methods can be used to handle child windows. The getWindowHandles method is used to store all the opened window handles in the Set data structure.
quit() are two different methods for closing the browser session in Selenium WebDriver . Understanding both of them and knowing when to use which method is important in your test execution. driver. close() – It closes the the browser window on which the focus is set.
To close a single browser window:
driver.close();
To close all (parent+child) browser windows and end the whole session:
driver.quit();
The logic you've used for switching the control to popup is wrong
for (String winHandle : driver.getWindowHandles()) { driver.switchTo().window(winHandle); }
How the above logic will swtich the control to new window ?
Use below logic to switch the control to new window
// 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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With