Python – In case of an error occurs inside do

I am running a process with Selenium and handling as many errors as I can and creating custom exceptions for few, but I have an issue if unexpected error does occur within process Selenium will keep running and take up resources/ram unless explicitly shut down, is there a better way to handle it than wrapping everything around try except and within except shutting down Selenium? much thanks

I would like to have some kind of “signal” in case error occurs send error to sentry io and free ram space from Selenium

simple example

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.example.com')
raise Exception()

if you dont explicitly say driver.quit() it will never shut down

dependinces https://pypi.org/project/webdriver-manager/ https://pypi.org/project/selenium/

one more example

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

class Example:
    def __init__(self):
        self.driver = webdriver.Chrome(ChromeDriverManager().install())
        self.test()

    def test(self):
        # Random unexpected error occurs
        # This case driver "doesnt" exist
        driver.get('https://www.example.com')

Example()

class is being ran from another file

I ended up doing this

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

_driver = webdriver.Chrome(ChromeDriverManager().install())

class Example:
    def __init__(self, driver):
        self.driver = driver
        self.test()

    def test(self):
        # Random unexpected error occurs
        # This case driver "doesnt" exist
        driver.get('https://www.example.com')

try:
    Example(_driver)
except Exception as e:
    print(e)
    _driver.quit()

Answer

Wrapping everything in a try/finally or a with statement sounds like exactly the right thing to do here…

Since Selenium supports with statements directly, that’s easiest:

from selenium import webdriver

with webdriver.Chrome() as wd:
    res = wd.get('https://stackoverflow.com/questions/')
    print(res.page_source)

(See also https://stackoverflow.com/a/54149666/683329)

The phrase to look for in documentation is “(use as a) context manager”.

Otherwise, if you’re working with something that either doesn’t doesn’t support the with statement, or you otherwise can’t, you can do try/finally:

wd = with webdriver.Chrome()
try:
    res = wd.get('https://stackoverflow.com/questions/')
    print(res.page_source) 
finally:
    wd.quit()

Edit: If you’re using a testing framework, it will have setup/cleanup functionality at module and class level and that cleanup will be run even if the tests fail.