'Webdriver - How to check if browser still exists or still open?

I want to check if browser still exists and if it isn't then i want to open a new browser! Is there a api available in webdriver to check if the browser still exists?



Solution 1:[1]

After calling driver.close() the value of driver is set to

FirefoxDriver: firefox on WINDOWS(4b4ffb1e-7c02-4d9c-b37b-310c771492ac)

But if you call driver.quit() then it sets the value of driver to

FirefoxDriver: firefox on WINDOWS (null)

So if you're checking the browser window after calling driver.quit() then you will be able to know by below implementation.

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.quit();              
if(driver.toString().contains("null"))
{

System.out.print("All Browser windows are closed ");
}
else
{
//open a new Browser
}

Solution 2:[2]

There is no api for it. The best one, you can do is call toString method, which returns a string like this:

SafariDriver . . . null

Then you can call contains method, which does check in the string null is there.

Note that this will work only if the quit is been called.

Solution 3:[3]

I actively use this for Chrome. At the same time, since I run the browsers with cmd title, I can close the command line to get rid of excessive loads.

from selenium.common.exceptions import WebDriverException

while True:
    try:
        #do somethings
    except selenium.common.exceptions.WebDriverException as e:
        if 'chrome not reachable' in str(e):
            os.system('taskkill /FI "WindowTitle eq YourTitleIfExistsOrDeleteThisLine*" /T /F')

Solution 4:[4]

public void isBrowserWindowOpen(WebDriver dr){
    RemoteWebDriver driver = (RemoteWebDriver) dr;
    try {
        driver.getWindowHandles();
    } catch (NullPointerException | NoSuchSessionException e) {
        //open a new Browser
    }
}

Solution 5:[5]

I've tried Arthur Kuklenko's concept for selenium in python:

try:
    driver.window_handles
    print("Driver has active window.")
except:
    print("Driver doesn't have active window.")

That worked great, but it put this warning message:

WARNING:urllib3.connectionpool:Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f7358c3bfa0>: Failed to establish a new connection: [Errno 111] Connection refused')': /session/b622db660ff0b436d0269368dd30bc7e
WARNING:urllib3.connectionpool:Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f7358c27d60>: Failed to establish a new connection: [Errno 111] Connection refused')': /session/b622db660ff0b436d0269368dd30bc7e
WARNING:urllib3.connectionpool:Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f7358c3b370>: Failed to establish a new connection: [Errno 111] Connection refused')': /session/b622db660ff0b436d0269368dd30bc7e

To ignore this message I've added logging:

import logging
import requests

logging.getLogger(requests.packages.urllib3.__package__).setLevel(logging.ERROR)

Finally the full code stands:

import logging
import requests
from selenium import webdriver

logging.getLogger(requests.packages.urllib3.__package__).setLevel(logging.ERROR)

driver = webdriver.Chrome()

try:
    driver.window_handles
    print("Driver has active window.")
except:
    print("Driver doesn't have active window.")

driver.quit()

try:
    driver.window_handles
    print("Driver has active window.")
except:
    print("Driver doesn't have active window.")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ajinkya
Solution 2 Ant's
Solution 3
Solution 4 Arthur Kuklenko
Solution 5 Mahmudur Rahman Shovon