8

Right now, I use this as a way to detect when the user closes the browser:

while True:
    try:
        # do stuff
    except WebDriverException:
        print 'User closed the browser'
        exit()

But I found out that it is very unreliable and a very bad solution since WebDriverException catches a lot of exception (if not all) and most of them are not due to the user closing the browser.

My question is: How to detect when the user closes the browser?

12
  • You can use Browser Unreachable exception. seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/…
    – Raj
    Commented Aug 4, 2018 at 12:03
  • HI, I am sorry I misunderstood that Exception is common across all the bindings, We use Selenium::WebDriver::Error::NoSuchWindowError in Ruby Selenium Binding, So I suggested you! Okay you may search the corresponding Exception for Python Binding. Commented Aug 4, 2018 at 12:16
  • Check out this discussion : stackoverflow.com/questions/18619121/… Commented Aug 4, 2018 at 12:24
  • @cruisepandey This discussion might not help, as here browser will be closed by user's action, not by calling driver.quit().
    – Raj
    Commented Aug 4, 2018 at 13:02
  • 2
    @CoreyGoldberg I can confirm that doesn't work. In fact, none of the browser object's properties appear to change when the user manually closes the browser. I was trying to catch that so I could re-open the browser programmatically, but I've given up. "What happens if you close the browser?" is like "What happens if you pull the plug on the computer?" or "What happens if you slam the keyboard over the monitor?" You can't guard against it in software.
    – RobertSF
    Commented Mar 27, 2019 at 20:13

4 Answers 4

8

I would suggest using:

>>> driver.get_log('driver')
[{'level': 'WARNING', 'message': 'Unable to evaluate script: disconnected: not connected to DevTools\n', 'timestamp': 1535095164185}]

since driver logs this whenever user closes browser window and this seems the most pythonic solution.

So you can do something like this:

DISCONNECTED_MSG = 'Unable to evaluate script: disconnected: not connected to DevTools\n'

while True:
    if driver.get_log('driver')[-1]['message'] == DISCONNECTED_MSG:
        print 'Browser window closed by user'
    time.sleep(1)

If you're interested you can find documentation here.

I am using chromedriver 2.41 and Chrome 68.

1
  • 1
    Very helpful answer
    – Polyvios P
    Commented Dec 18, 2019 at 4:20
5

Similar to Federico Rubbis answer, this worked for me:

import selenium
from selenium import webdriver
browser = webdriver.Firefox()

# Now wait if someone closes the window
while True:
    try:
        _ = browser.window_handles
    except selenium.common.exceptions.InvalidSessionIdException as e:
        break
    time.sleep(1)

# ... Put code here to be executed when the window is closed

Probably not a solution that is stable across versions of selenium.
EDIT: Not even stable across running from different environments. Ugly fix: Use BaseException instead of selenium.common.exceptions.InvalidSessionIdException .

1
  • I used selenium.common.exceptions.WebDriverException thanks. Commented May 14 at 12:29
0

this worked for me:

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

#open browser in kiosk mode
def openBrowser():
    chrome_options = Options()
    chrome_options.add_argument("--kiosk")
    path = '"C:\\Users\\an8799\Documents\Repositorio\\testes\chromedriver.exe"'
    driver = webdriver.Chrome(path, chrome_options=chrome_options)
    driver.get('https://google.com.br')
    return driver

#verify if browser is opened.
def uptdateBrowser(browser):
    try:
       _ = browser.window_handles
       print('windon opened')
       return 0, 0
    except:
        newbrowser = openBrowser()
        print('window closed')
        return 1, newbrowser

#start browser verification loop
def main():
    browser = openBrowser()
    while(True):
        flagNew, newB = uptdateBrowser(browser)
        if flagNew == 1:
            browser = newB
#call main loop
if __name__ == '__main__':
    main()
0

Although Federico Rubbi answer works, it doesn't work always. When the user closes the webdriver, the driver.get_log('driver') will raise an excepton.

The following function works quite well, as far as i tested it (Chrome version 110). It also works with undetected chromedriver.

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from urllib3.exceptions import NewConnectionError, MaxRetryError


def is_driver_open(driver: webdriver.Chrome) -> bool:
    """
    Checks if the webdriver is still open by checking the logs.
    """
    disconnected_msg = 'Unable to evaluate script: disconnected: not connected to DevTools\n'
    disc_msg = "Unable to evaluate script: no such window: target window already closed" \
               "\nfrom unknown error: web view not found\n"
    message_listener = "subscribing a listener to the already connected DevToolsClient"
    if driver:
        try:
            log = driver.get_log('driver')
        except (ConnectionRefusedError, MaxRetryError, NewConnectionError):
            # The webdriver is closed, the connection to the Chrome is refused.
            return False
        print(f"is_driver_open(): log: {log}")
        if len(log) != 0:  # This does not catch all other messages.
            if log[-1]['message'] in (disconnected_msg, disc_msg):
                print("Webdriver is closed")
                return False
            elif message_listener in log[-1]['message']:
                # It's not closed.
                return True
            else:
                return True
        else:  # No errors, return True.
            return True


if __name__ == "__main__":
    options = Options()
    driver = webdriver.Chrome(options=options)
    print(f"Is driver open: {is_driver_open(driver)}")
    driver.quit()
    print(f"Is driver open: {is_driver_open(driver)}")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.