About this lesson
Selenium Python with webdriver.Chrome: quit the browser using a context manager
Selenium Python AssignmentsAdvancedStructure
How it works
A with statement runs setup code, runs your block, and then runs cleanup code, even when the block fails. The driver class supports this. Entering the block gives you the driver, and leaving it calls quit for you. The result is the same safety as try and finally, with no separate quit line to remember.
The name after as is the driver you use inside the block. Once the block ends, the browser is gone. Any call made after it fails, for example with a connection error from the urllib3 library, since the chromedriver process it talks to has stopped. So keep all browser work inside the indented block.
The cleanup runs on an error too. If a locator raises NoSuchElementException halfway through, Python still exits the with block, quits Chrome, and only then shows the traceback. That stops stray browser processes from piling up on a test machine. You can read driver.title as an attribute inside the block, and join it to a label with + because it is already a string.
About the assignment
Start the browser with a with statement, so there is no explicit finally and no manual quit call. Open the Text Fields page and print its title with the label from the task.
What you will do
- Import
webdriveras in other lessons, then begin the block withwithand the Chrome constructor, naming the driver withas. - Inside the indented block, load the Text Fields page.
- Still inside the block, read the title attribute and print it after its label.
- Do not add a quit call. Leaving the block does it.
Common mistakes
Using the driver after the block
Once the indentation ends, Chrome is closed. A later call fails with
MaxRetryError, as Python cannot reach chromedriver any more.Forgetting the colon or the indent
The statement ends with a colon and the body must be indented. Missing either gives a
SyntaxErroror anIndentationErrorbefore Chrome even starts.Adding quit as well
It does no harm in many cases, but it hides the point of the lesson. Pick one cleanup style and keep it.
In a real test suite
Most suites do not put with in every test. A pytest fixture creates the driver, yields it, and quits after the test. That is the same idea, written once in conftest.py, and it is why a failing test never leaves a browser open.
The next lesson groups locator code into a small class.
Related lessons
- Open Chrome and Read the Page TitleOpens Chrome and reads the title with an explicit quit, the version this lesson replaces.
- Fix a Locator That Does Not ExistRaises
NoSuchElementExceptionon purpose, the kind of failure cleanup must survive. - Build a One-File Page ObjectWraps page actions in a class once cleanup is sorted.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
# Open Chrome in a context manager, open https://testkru.com/Elements/TextFields, and print the title as "Title: ..."