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
- Find the left-click button with the id strategy and keep the element in a variable.
- Read
.textand print it after the first label from the task. - Call
click()on the same element. - Read
.textagain 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
.textagain after the click.Writing .text as a method
Brackets after
.textraiseTypeError: 'str' object is not callable, because the property has already given you a string. Only methods likeclick()take brackets.Reusing an element after a reload
After a refresh, a saved element raises
StaleElementReferenceExceptionthe 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.
Related lessons
- Assert the Text You Just ReadRepeats this click, then turns the text comparison into an
assertstatement. - Read Visible TextLooks at
.textin detail, including what counts as visible text. - Wait Until a Button Is ClickableWaits until a button can be clicked before pressing 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")
# Print #leftClick before and after click()
finally:
driver.quit()