Skip to content
CodeKruPro

Click a Button and Read the New Text

18/80

Click a Button and Read the New Text

click() fires the click. Read .text before and after so you see what the page changed.

Task:

  1. Open Buttons
  2. Print the text of #leftClick
  3. Click it and print the text again

Print the lines below exactly.

Expected output:

Before: Left click on me
After: I was left-clicked!
Read full tutorial on Codekru

About this lesson

Selenium Python click button: use click() and compare the text before and after

Selenium Python AssignmentsBeginnerWeb Element Interactions

How it works

Once you hold an element, click() presses it the way a mouse would. In Selenium Python it takes no arguments and returns None. It does not tell you what the page did in response, so you learn that by reading something afterwards. Reading the text before and after is a small proof that the click had an effect.

The same WebElement variable stays valid after the click, as long as that element stays on the page. Here the click only changes the button text, so .text on the old variable shows the new words. After a refresh or a navigation, the old variable is stale and touching it raises StaleElementReferenceException.

.text is a property, so you write it without brackets, and it returns a str. Store the button in a variable once and read .text each time you need the current words. A string you saved earlier does not update by itself.

About the assignment

On the Buttons page, press the left-click button and prove that the page responded. You print its text once before pressing and once after.

What you will do

  1. Find the left-click button with the id strategy and keep the element in a variable.
  2. Read .text and print it after the first label from the task.
  3. Call click() on the same element.
  4. Read .text again from the same variable and print it after the second label.

Common mistakes

  • Reusing the saved string

    If you store the text before the click and print it twice, both lines show the old words. Call .text again after the click.

  • Writing .text as a method

    Brackets after .text raise TypeError: 'str' object is not callable, because the property has already given you a string. Only methods like click() take brackets.

  • Reusing an element after a reload

    After a refresh, a saved element raises StaleElementReferenceException the next time you touch it. Find it again once the page has reloaded.

In a real test suite

In a pytest suite a click is rarely the end of a test. It is followed by a check of what changed, and clicks usually go through a helper that waits until the button can be pressed. A page object method such as press_left() hides the locator.

The next lesson turns that before-and-after comparison into an assert.

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")
    # Print #leftClick before and after click()
finally:
    driver.quit()
Read the full tutorial