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?
The close() method is a Webdriver command that closes the browser window currently in focus. It is best to use the close() command when multiple browser tabs or windows are open. If only one window is open in the entire browser, then the close() command will quit the entire browser session.
close() and driver. quit() are two methods for closing a browser session in Selenium WebDriver.
quit() method closes all the browsers. close() method closes the active WebDriver instance. quit() method closes all the active WebDriver instances.
1 Answer. so you could check for null when assigning a boolean: boolean hasQuit = driver. toString().
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 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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With