Skip to content
CodeKruPro

Assert the Text You Just Read

19/80

Assert the Text You Just Read

assert stops the script when a condition is false. Use it after you have read the text the page shows.

Task:

  1. Click #leftClick
  2. Assert that its text is I was left-clicked!
  3. Print that text

Print the lines below exactly.

Expected output:

After: I was left-clicked!

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

  1. Find the left-click button and call click() on it.
  2. Read .text into a variable, so you check and print the same string.
  3. Write an assert that compares that variable with the text the task names, punctuation and case included.
  4. 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 AssertionError with 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.

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
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()