About this lesson
Selenium Python element_to_be_clickable: wait for a button before you click it
Selenium Python AssignmentsIntermediateWaits
How it works
A button can be in the page and still not ready. It might be hidden or disabled. EC.element_to_be_clickable waits until the element is displayed and enabled, then returns it. With WebDriverWait(driver, 10).until(...) you get back a WebElement you can click at once.
It expects one tuple holding the By strategy and the locator string. Pass them separately and Python raises TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given. If the id is not on the page, the wait runs to its limit and raises TimeoutException.
The condition does not click for you. You still call click(). Afterwards the element may change: on this page the left-click button turns disabled once clicked, and its text changes. The same variable still points at the button, so text reads the new label with no second find.
That change has a consequence. A second wait for clickable on the same button never succeeds, because it is now disabled, and ends in TimeoutException. One wait per action is the safe pattern.
To print "displayed and enabled" as one answer, join the two checks with and. The result is a bool, so use str() before it follows a label.
About the assignment
Open the Buttons page, wait until the left-click button is ready, report that it is both visible and enabled, click it, and print the text it shows afterwards.
What you will do
- Wait up to ten seconds for the button with
element_to_be_clickable, giving it a tuple with the id. - Combine
is_displayed()andis_enabled()into one answer and print it as text after the first label. - Click the element that the wait returned.
- Read the same element's
textand print it after the second label.
Common mistakes
Waiting for clickable twice
After the click the button is disabled. A second wait on it ends in
TimeoutExceptionwith an empty message. Reuse the element you already have.Using the wrong button id
The page has several buttons, including double-click, right-click and a permanently disabled one. Pick the id for the disabled one and the wait times out.
Joining the bool directly
The result of
andbetween two checks is abool. Adding it to a string raisesTypeError. Wrap it instr().
In a real test suite
Teams build a click_when_ready helper in a page object, so every click goes through the wait and tests stay short.
The next lesson reverses the wait: instead of an element that arrives, it waits for one that leaves.
Related lessons
- Click a Button and Read the New TextClick a button and read its new text, now with a wait before the click.
- Wait Until an Element Is VisibleUse
visibility_of_element_located, the simpler condition this one builds on. - Check Whether a Text Field Is EnabledCheck
is_enabled(), which this wait applies for you.
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
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/Buttons")
# Wait until #leftClick is clickable, print whether it is displayed and enabled, then click
finally:
driver.quit()