Skip to content
CodeKruPro

Open Chrome and Read the Page Title

1/80

Open Chrome and Read the Page Title

A script can open Chrome, open a page, and print the title the browser reports. Quit the browser in finally so it closes even when a line raises an error.

Task:

  1. Open https://testkru.com/Elements/TextFields
  2. Print the title on one line
  3. Quit inside finally

Before Selenium, you need variables, strings, lists, dicts, functions, if, for, print, and the ability to read a short traceback.

Print the lines below exactly.

Expected output:

Title: Text Fields

About this lesson

Selenium Python: open Chrome and get the page title with driver.title

Selenium Python AssignmentsBeginnerGetting Started

How it works

A Selenium script is an ordinary Python program that controls a real browser. The line that builds webdriver.Chrome() starts the chromedriver helper program, opens a Chrome window and gives you back a driver object. Every later call on that object is a command sent to the browser.

The title is a good first thing to read because it needs no locator. In Python it is a property, so you write driver.title with no parentheses. Calling it like a function fails.

A fresh session starts on an empty page, and the title there is an empty string. Load the address with driver.get() first. That call returns None and only comes back once the page has finished loading.

About the assignment

The starter already creates the driver and wraps the work in try and finally. You fill in the middle: open the Text Fields practice page, read the title the browser reports, and show it on a single labelled line.

What you will do

  1. Open the page given in the task with driver.get() and the full address.
  2. Read the title from the driver. It is already text, so no conversion is needed.
  3. Print one line made of the label Title:, one space, and the title.
  4. Do not touch the finally block. It must keep the quit() call that closes Chrome.

Common mistakes

  • Adding parentheses to the title

    Writing driver.title() raises TypeError: 'str' object is not callable. The title is an attribute that already holds a string, so leave the brackets off.

  • Opening a page with a relative path or no scheme

    A value such as /Elements/Buttons or testkru.com/... makes get() raise InvalidArgumentException: invalid argument. Always give the whole address, starting with https://.

  • Putting quit() outside finally

    If any line before it raises, quit() is skipped. Chrome then closes only if Selenium's cleanup runs as Python exits, and a hard kill leaves it running. finally closes it for certain. Keeping quit() in finally closes the browser on success and on failure alike.

In a real test suite

In a pytest suite the driver is usually built once in a fixture inside conftest.py. The fixture yields the driver to the test and calls quit() after the yield, which does the same job as your finally block, only shared by every test.

The next lesson keeps the same driver alive and loads a second page, so you can see how titles and addresses change as you move around.

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

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/TextFields")
    # Print the title as "Title: ..."
finally:
    driver.quit()