Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 419
Rahul Avatar asked Feb 09 '16 07:02

Rahul


People also ask

How do I close all browser windows in Selenium?

close() closes only the current window on which Selenium is running automated tests. The WebDriver session, however, remains active. On the other hand, the driver. quit() method closes all browser windows and ends the WebDriver session.

How do I close a browser session without killing WebDriver?

manually insert cookies into Firefox browser session when you open new session. That means you have to extract all cookies before close(), persist them in memory or on file on disk, then load them back into WebDriver on next startup via createCookie method, etc.

Which is the method to close the browser instance?

close() method is used to close the current browser window on which the focus is set, on the other hand quit() method essentially calls the driver. dispose method that successively closes all the browser windows and ends the WebDriver session graciously. Action performed : close() method closes the current window.

What does driver close () do?

driver. close() : The close() method closes the currently focused window, quitting the driver if the current window is the only open window. If there are no windows open, it will error out.


2 Answers

You should understand difference between driver.close() and driver.quit()

driver.close() and driver.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.

driver.quit() – It basically calls driver.dispose method which in turn closes all the browser windows and ends the WebDriver session gracefully.

You should use driver.quit() whenever you want to end the program. It will close all opened browser window and terminates the WebDriver session. If you do not use driver.quit at the end of program, WebDriver session will not close properly and files would not be cleared off memory. This may result in memory leak errors.

In your case you have to use driver.close() which will close current window and keeps driver active.

Just to add - if there is only browser window open and you use driver.close(), it will quit the webdriver session. The webdriver will not stay active.

like image 93
Sanjay Bhimani Avatar answered Sep 25 '22 15:09

Sanjay Bhimani


The below explanation should explain the difference between driver.close and driver.quit methods in WebDriver. I hope you find it useful.

driver.close and driver.quit are two different methods for closing the browser session in Selenium WebDriver.

Understanding both of them and knowing when to use each method is important in your test execution. Therefore, I have tried to shed some light on both of these methods.

driver.close - This method closes the browser window on which the focus is set. driver.quit close the session of webdriver while driver.close only close the current window on which selenium control is present but webdriver session not close yet, if no other window open and you call driver.close then it also close the session of webdriver.

driver.quit – This method basically calls driver.dispose a now internal method which in turn closes all of the browser windows and ends the WebDriver session gracefully.

driver.dispose - As mentioned previously, is an internal method of WebDriver which has been silently dropped according to another answer - Verification needed. This method really doesn't have a use-case in a normal test workflow as either of the previous methods should work for most use cases.

Explanation use case: You should use driver.quit whenever you want to end the program. It will close all opened browser windows and terminates the WebDriver session. If you do not use driver.quit at the end of the program, the WebDriver session will not close properly and files would not be cleared from memory. This may result in memory leak errors.

............

Now In that case you need to specific 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.

like image 39
Shubham Jain Avatar answered Sep 22 '22 15:09

Shubham Jain