Selenium: python contextmanager for webdriver

Created on 3 Dec 2019  路  5Comments  路  Source: SeleniumHQ/selenium

馃殌 Feature Proposal

Adding __enter__ and __exit__ methods to python webdriver will make it as context manger

Motivation

Eliminates the use of quit(), as with file handling in python. Remains backward compatible with usual way of calling quit saperately

Example

with webdriver.Chrome() as driver:
    driver.get("some/address")
    driver.execute_script("some_js_script")

Code

class WebDriver:
...
def __enter__(self):
    return self

def __exit__(self, exc_type, exc_val, exc_tb):
    do_something_with_the_execeptions_if_any(exc_type, exc_val, exc_tb)
    self.quit()

Most helpful comment

@diemol Yes please close.

All 5 comments

@lmtierney @AutomatedTester @Dude-X
Do you have any thoughts?

@dumbPy This has been implemented since version 3.13.0. The latest version has context manager support.

In a testing context, I find a context manager useless, but other forms of scripting may be useful

Ah ok, so this already exists @Dude-X? We could close this feature request, just confirming.

@diemol Yes please close.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings