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
- Open the page given in the task with
driver.get()and the full address. - Read the title from the driver. It is already text, so no conversion is needed.
- Print one line made of the label
Title:, one space, and the title. - Do not touch the
finallyblock. It must keep thequit()call that closes Chrome.
Common mistakes
Adding parentheses to the title
Writing
driver.title()raisesTypeError: '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/Buttonsortestkru.com/...makesget()raiseInvalidArgumentException: invalid argument. Always give the whole address, starting withhttps://.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.finallycloses it for certain. Keepingquit()infinallycloses 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.
Related lessons
- Visit Two Pages with driver.get()Load a second page in the same session and read its address as well as its title.
- Go Back and ForwardUse the browser history to return to the first page without typing its address again.
- Set the Window SizeSet the window size, another setting you will want in place before a test starts.
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
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/TextFields")
# Print the title as "Title: ..."
finally:
driver.quit()