How to close the whole browser window by keeping the webDriver active?

0
0

In my batch execution, multiple browsers with multiple tabs are getting opened for first scenario. I wanted to close all these browsers before starting second scenario.

Driver.close() is just closing one tab of the browser.
Driver.quit() is closing all the browsers and also ending the WebDriver session. So , am unable to run the batch execution. Please provide a solution for this.

  • You must to post comments
0
0

In that case you need to know the id of browser.
Below is code which will close all the child windows except the Main window.

String homeWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
//Use Iterator to iterate over windows
Iterator<String> windowIterator = allWindows.iterator();
//Verify next window is available
while(windowIterator.hasNext())
{
//Store the Recruiter window id
String childWindow = windowIterator.next();
}
//Here we will compare if parent window is not equal to child window
if (homeWindow.equals(childWindow))
{
driver.switchTo().window(childWindow);
driver.close();
}

Now here you need to modify or add the condition according to your need

if (homeWindow.equals(childWindow))
{
driver.switchTo().window(childWindow);
driver.close();
}

Currently it is checking only if home window is equal to childwindow or not. Here you need to specify the condition like which id’s you want to close. I never tried it so just suggested you the way to achive your requirement.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website