Webdriver - How to check if browser still exists or still open?
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}
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.
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 WebDriverExceptionwhile 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')