Webdriver - How to check if browser still exists or still open?
Asked Answered
D

5

19

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?

Disorient answered 23/12, 2014 at 8:15 Comment(0)
B
9

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
}
Buckeye answered 23/12, 2014 at 8:43 Comment(2)
This is a terrible idea. Checking the toString method of anything to determine application logic is shooting yourself in the foot.Immotile
@Immotile Any better idea? I can't find any api to check the status.Shabuoth
D
3

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.

Deputy answered 23/12, 2014 at 9:24 Comment(0)
P
2

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')
Parnas answered 25/3, 2020 at 19:5 Comment(0)
H
2
public void isBrowserWindowOpen(WebDriver dr){
    RemoteWebDriver driver = (RemoteWebDriver) dr;
    try {
        driver.getWindowHandles();
    } catch (NullPointerException | NoSuchSessionException e) {
        //open a new Browser
    }
}
Haily answered 27/12, 2021 at 13:9 Comment(0)
A
0

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.")
Afeard answered 5/4, 2022 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.