Skip to content
CodeKruPro

Quit the Browser with a Context Manager

77/80

Quit the Browser with a Context Manager

with webdriver.Chrome() as driver quits the browser when the block ends, including when a line raises an error. Use this after finally is already a habit.

Task:

  1. Open Chrome with a with statement
  2. Open Text Fields
  3. Print the title

Print the lines below exactly.

Expected output:

Title: Text Fields

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

  1. Import webdriver as in other lessons, then begin the block with with and the Chrome constructor, naming the driver with as.
  2. Inside the indented block, load the Text Fields page.
  3. Still inside the block, read the title attribute and print it after its label.
  4. 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 SyntaxError or an IndentationError before 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.

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.

main.py
from selenium import webdriver

# Open Chrome in a context manager, open https://testkru.com/Elements/TextFields, and print the title as "Title: ..."