Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 490
user2048204 Avatar asked Dec 23 '14 08:12

user2048204


People also ask

How do I close and reopen a browser in Selenium WebDriver?

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.

How do I close an already opened Chrome browser in Selenium?

close() and driver. quit() are two methods for closing a browser session in Selenium WebDriver.

Which method closes the open browser in WebDriver?

quit() method closes all the browsers. close() method closes the active WebDriver instance. quit() method closes all the active WebDriver instances.

How do I check if a driver is null?

1 Answer. so you could check for null when assigning a boolean: boolean hasQuit = driver. toString().


3 Answers

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
}
like image 191
Rupesh Shinde Avatar answered Oct 11 '22 00:10

Rupesh Shinde


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.

like image 21
Ant's Avatar answered Oct 11 '22 00:10

Ant's


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')
like image 36
Fazıl Akbulut Avatar answered Oct 11 '22 01:10

Fazıl Akbulut