In Python, How to Check If Selenium Webdriver Has Quit or Not

In Python, how to check if Selenium WebDriver has quit or not?

You can use something like this which uses psutil

from selenium import webdriver
import psutil

driver = webdriver.Firefox()

driver.get("http://tarunlalwani.com")

driver_process = psutil.Process(driver.service.process.pid)

if driver_process.is_running():
print ("driver is running")

firefox_process = driver_process.children()
if firefox_process:
firefox_process = firefox_process[0]

if firefox_process.is_running():
print("Firefox is still running, we can quit")
driver.quit()
else:
print("Firefox is dead, can't quit. Let's kill the driver")
firefox_process.kill()
else:
print("driver has died")

In Java, best way to check if Selenium WebDriver has quit

If quit() has been called, driver.toString() returns null:

>>> FirefoxDriver: firefox on XP (null))

Otherwise, it returns a hashcode of the object:

>>> FirefoxDriver: firefox on XP (9f897f52-3a13-40d4-800b-7dec26a0c84d)

so you could check for null when assigning a boolean:

boolean hasQuit = driver.toString().contains("(null)");

chromedriver.quit and chromedriver.close doesn't work

There's already good result above that you should use quit(), not quit, and close(), not close,

as well try to use Options() instead of ChromeOptions()

so, the code looks like that:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = '/usr/bin/google-chrome'
driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=options) #Chrome has opened
driver.quit()

Python Selenium: How to check whether the WebDriver did quit()?

If you would explore the source code of the python-selenium driver, you would see what the quit() method of the firefox driver is doing:

def quit(self):
"""Quits the driver and close every associated window."""
try:
RemoteWebDriver.quit(self)
except (http_client.BadStatusLine, socket.error):
# Happens if Firefox shutsdown before we've read the response from
# the socket.
pass
self.binary.kill()
try:
shutil.rmtree(self.profile.path)
if self.profile.tempfolder is not None:
shutil.rmtree(self.profile.tempfolder)
except Exception as e:
print(str(e))

There are things you can rely on here: checking for the profile.path to exist or checking the binary.process status. It could work, but you can also see that there are only "external calls" and there is nothing changing on the python-side that would help you indicate that quit() was called.

In other words, you need to make an external call to check the status:

>>> from selenium.webdriver.remote.command import Command
>>> driver.execute(Command.STATUS)
{u'status': 0, u'name': u'getStatus', u'value': {u'os': {u'version': u'unknown', u'arch': u'x86_64', u'name': u'Darwin'}, u'build': {u'time': u'unknown', u'version': u'unknown', u'revision': u'unknown'}}}
>>> driver.quit()
>>> driver.execute(Command.STATUS)
Traceback (most recent call last):
...
socket.error: [Errno 61] Connection refused

You can put it under the try/except and make a reusable function:

import httplib
import socket

from selenium.webdriver.remote.command import Command

def get_status(driver):
try:
driver.execute(Command.STATUS)
return "Alive"
except (socket.error, httplib.CannotSendRequest):
return "Dead"

Usage:

>>> driver = webdriver.Firefox()
>>> get_status(driver)
'Alive'
>>> driver.quit()
>>> get_status(driver)
'Dead'

Another approach would be to make your custom Firefox webdriver and set the session_id to None in quit():

class MyFirefox(webdriver.Firefox):
def quit(self):
webdriver.Firefox.quit(self)
self.session_id = None

Then, you can simply check the session_id value:

>>> driver = MyFirefox()
>>> print driver.session_id
u'69fe0923-0ba1-ee46-8293-2f849c932f43'
>>> driver.quit()
>>> print driver.session_id
None

Selenium Webdriver sessionId or check if all browser windows are closed

Just set

driver=null; 

everytime you quit the browser and than check

if (browser!=null){
//Attention: this comand is not supported
//as far as i know ;)
driver.doSomething();
}

or

try{


}catch (NullPointerException e)

e.printStackTrace();
System.err.print"DAMN";
}

or receive a NullPointerException ;)



Related Topics



Leave a reply



Submit