Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to control whether my WebDriver quit but I can't find a method for that. (It seems that in Java there's a way to do it)

from selenium import webdriver
driver = webdriver.Firefox()
driver.quit()
driver # <selenium.webdriver.firefox.webdriver.WebDriver object at 0x108424850>
driver is None # False

I also explored the attributes of WebDriver but I can't locate any specific method to get information on the driver status. Also checking the session id:

driver.session_id # u'7c171019-b24d-5a4d-84ef-9612856af71b'
like image 745
CptNemo Avatar asked Mar 09 '15 02:03

CptNemo


People also ask

What does quit () do in selenium?

quit() method closes all browser windows and ends the WebDriver session.

What is the correct answer regarding driver close () driver quit ()?

Your answer driver. quit() is used to exit the browser, end the session, tabs, pop-ups etc. But the when you driver. close(), only the window that has focus is closed.

How do I check if a driver is null?

so you could check for null when assigning a boolean: boolean hasQuit = driver. toString(). contains("(null)");


2 Answers

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
like image 134
alecxe Avatar answered Oct 09 '22 05:10

alecxe


This work for me:

from selenium import webdriver

driver = webdriver.Chrome()
print( driver.service.is_connectable()) # print True

driver.quit()
print( driver.service.is_connectable()) # print False
like image 42
Matias Bertani Avatar answered Oct 09 '22 06:10

Matias Bertani