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?
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')
public void isBrowserWindowOpen(WebDriver dr){
RemoteWebDriver driver = (RemoteWebDriver) dr;
try {
driver.getWindowHandles();
} catch (NullPointerException | NoSuchSessionException e) {
//open a new Browser
}
}
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.")
© 2022 - 2024 — McMap. All rights reserved.
toString
method of anything to determine application logic is shooting yourself in the foot. – Immotile