Skip to content
CodeKruPro

Wait Until a Button Is Clickable

40/80

Wait Until a Button Is Clickable

element_to_be_clickable waits until the element is shown and enabled. Then you can click it.

Task:

  1. Wait until #leftClick is clickable
  2. Print whether it is displayed and enabled
  3. Click it and print the new text

Print the lines below exactly.

Expected output:

Button is clickable: True
After click: I was left-clicked!

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

  1. Wait up to ten seconds for the button with element_to_be_clickable, giving it a tuple with the id.
  2. Combine is_displayed() and is_enabled() into one answer and print it as text after the first label.
  3. Click the element that the wait returned.
  4. Read the same element's text and 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 TimeoutException with 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 and between two checks is a bool. Adding it to a string raises TypeError. Wrap it in str().

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.

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