Well, it's python, you can make your own closing
class, based on contextlib.closing
and override __exit__()
method:
import contextlib
from selenium import webdriver
class closing(contextlib.closing):
def __exit__(self, *exc_info):
self.thing.quit()
with closing(webdriver.Firefox()) as browser:
browser.get('http://stackoverflow.com')
FYI, there was a proposal to make webdriver
a context manager, but it was closed as won't fix
since quit()
is the right way to close the browser, and it should be called explicitly, see Make webdriver a context manager [python bindings].