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

4

22

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.

Wield answered 9/2, 2016 at 7:20 Comment(1)
Possible duplicate of Closing all opened tabs except the first tab/main tab using webdriverRetardment
T
13

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.

Taro answered 9/2, 2016 at 7:29 Comment(0)
A
42

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.

Airy answered 9/2, 2016 at 7:36 Comment(1)
browser window mean tab?Acerbic
T
13

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.

Taro answered 9/2, 2016 at 7:29 Comment(0)
W
1

This code closes all the opened windows and then brings back control to the main window.

public static void switchTab() {            
    try {
        Set<String> windows = webDriver.getWindowHandles();
        Iterator<String> iter = windows.iterator();
        String[] winNames=new String[windows.size()];
        int i=0;
        while (iter.hasNext()) {
            winNames[i]=iter.next();
            i++;
        }

        if(winNames.length > 1) {
            for(i = winNames.length; i > 1; i--) {
                webDriver.switchTo().window(winNames[i - 1]);
                webDriver.close();
            }
        }
        webDriver.switchTo().window(winNames[0]);
    }
    catch(Exception e){         
        e.printStackTrace();
    }
}
Wield answered 10/2, 2016 at 12:2 Comment(1)
How can it bring control back to the main window if it's closed?Blockhouse
L
0

My problem was, that when I opened a lot of URLs in the loop, I got a lot of open windows that are memory- and processor-consuming.

Attempt to use webDriver.close() in the end of a loop resulted in org.openqa.selenium.NoSuchSessionException: Tried to run command without establishing a connection - webDriver called quit() cause only one window was opened in first loop run (look top answer for explanation).

Final solution:

   if (webDriver.getWindowHandles().size() > 1){
        webDriver.close();
    }
Lambaste answered 26/1, 2022 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.