About this lesson
Selenium Python assert text: check what the page shows with assert and AssertionError
Selenium Python AssignmentsBeginnerWeb Element Interactions
How it works
A script that only prints cannot tell you that something went wrong. An assert statement can. It evaluates a condition, and when the condition is false Python raises AssertionError and the script stops at that line. When it is true nothing happens. This is the same mechanism pytest builds on, so the habit carries over.
The usual shape compares the text you read from the page with the text you expect. The comparison is exact, so differences in capital letters or punctuation make it false. You may add a message after a comma, and Python shows it as the text of the error. Read the text after the click, because the button changes only once the click has happened.
There is one Python trap. If you wrap the condition and the message together in brackets, you have written a tuple, and a non-empty tuple is always true. The assertion can then never fail, and Python only gives a SyntaxWarning. Write the condition, a comma, then the message, with no outer brackets.
About the assignment
On the Buttons page, press the left-click button, assert that the text it now shows is the expected one, and only then print that text.
What you will do
- Find the left-click button and call
click()on it. - Read
.textinto a variable, so you check and print the same string. - Write an
assertthat compares that variable with the text the task names, punctuation and case included. - Print the variable after the label from the task.
Common mistakes
Asserting a tuple
An assert whose condition and message sit inside one pair of brackets always passes, because the tuple is non-empty. Python only warns with
SyntaxWarning. Put the message after a comma, outside brackets.Comparing with the wrong case
The check is exact, so a lowercase first letter makes it false and raises
AssertionError. Copy the expected wording from the task, including the exclamation mark.Asserting before the click
Before the click the button still shows its old words, so the assert raises
AssertionErrorwith an empty message. Add a message that includes the actual text, and the failure explains itself.
In a real test suite
Plain assert is what pytest tests use, and pytest prints both sides of a failed comparison for you. In larger suites teams wrap the read and the check in one helper, so each test shows only the expected value.
The next lesson stops reading buttons and starts writing: it types into a text field with send_keys.
Related lessons
- Click a Button and Read the New TextThe same click and the same text, printed without any assertion.
- Read Visible TextReads visible text from several kinds of elements.
- Diagnose a Wait That Times OutA wait that times out raises an exception too, and that lesson shows how to diagnose it.
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
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/Buttons")
# Click #leftClick, assert the new text, then print it
finally:
driver.quit()