Waits and Conditions

Loading concept...

Selenium Waits and Conditions: The Art of Patience

The Restaurant Analogy 🍽️

Imagine you walk into a busy restaurant. You can’t just grab food the moment you sit down—you have to wait for things to happen:

  • The waiter needs to appear at your table
  • The menu needs to be visible before you can read it
  • Your food needs to be ready before you can eat

Selenium works the same way! Web pages don’t load instantly. Elements appear at different times. If you try to click a button before it exists, your code crashes. Waits are how we tell Selenium: “Be patient. Wait for things to be ready.”


Why Do We Need Waits?

Think about ordering food online:

  1. You click “Add to Cart” 🛒
  2. A loading spinner appears ⏳
  3. The cart updates with your item âś…

If your Selenium code tries to check the cart before the spinner finishes, it won’t find your item! The page wasn’t ready yet.

graph TD A[Click Button] --> B[Page Starts Loading] B --> C{Is Element Ready?} C -->|No| D[Wait...] D --> C C -->|Yes| E[Interact with Element!]

The Two Types of Waits

🔵 Implicit Wait: The Polite Wait

What is it? You tell Selenium: “If you can’t find something, wait up to X seconds before giving up.”

It’s like telling a waiter: “I might not be ready to order immediately. Give me up to 10 seconds to decide.”

Simple Example:

from selenium import webdriver

driver = webdriver.Chrome()

# Set implicit wait for 10 seconds
driver.implicitly_wait(10)

# Now every find_element will wait
# up to 10 seconds if needed
element = driver.find_element("id", "myButton")

Key Points:

  • Set it once at the start
  • Applies to all element searches
  • Waits only if element isn’t immediately found
  • Like a default patience level for your whole script

đźź  Explicit Wait: The Specific Wait

What is it? You tell Selenium: “Wait for THIS specific thing to happen, then continue.”

It’s like telling the waiter: “Don’t bring me the check until I’ve finished my dessert.”

Why use it?

  • More control than implicit waits
  • Wait for specific conditions (visible, clickable, etc.)
  • Different waits for different elements

Simple Example:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://example.com")

# Wait up to 10 seconds for button to be clickable
wait = WebDriverWait(driver, 10)
button = wait.until(
    EC.element_to_be_clickable((By.ID, "submitBtn"))
)
button.click()

WebDriverWait: Your Patience Tool

WebDriverWait is the magic wand that makes explicit waits work.

How It Works

graph TD A[Start WebDriverWait] --> B[Check Condition] B --> C{Condition Met?} C -->|Yes| D[Return Element] C -->|No| E{Time Left?} E -->|Yes| F[Wait 0.5 seconds] F --> B E -->|No| G[Timeout Error!]

The Recipe

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Create a wait object
# driver = your browser
# 10 = maximum seconds to wait
wait = WebDriverWait(driver, 10)

# Use .until() with a condition
element = wait.until(
    EC.some_condition((By.LOCATOR, "value"))
)

Think of it like a timer ⏱️:

  • Checks every 0.5 seconds (by default)
  • Stops when condition is true
  • Gives up after your time limit

The Six Essential Conditions

These are the questions Selenium can ask while waiting:

1. đź‘€ Visibility Condition

Question: “Can I SEE this element on the screen?”

An element might exist in the HTML but be hidden. This waits until it’s actually visible.

# Wait until element is visible
element = wait.until(
    EC.visibility_of_element_located(
        (By.ID, "welcomeMessage")
    )
)

Real Example: A loading screen disappears, and a welcome message fades in.


2. 📍 Presence Condition

Question: “Does this element EXIST in the page?”

It doesn’t need to be visible—just present in the HTML.

# Wait until element exists in DOM
element = wait.until(
    EC.presence_of_element_located(
        (By.CSS_SELECTOR, ".hidden-data")
    )
)

When to use: Checking for hidden elements, or elements that load but aren’t shown yet.

Difference from Visibility:

Presence Visibility
Just exists in code Must be seen on screen
Can be hidden Cannot be hidden
Faster check Checks display & size

3. 👆 Clickability Condition

Question: “Can I CLICK this element right now?”

An element must be:

  • Present âś…
  • Visible âś…
  • Enabled âś…
  • Not covered by another element âś…
# Wait until button is clickable
button = wait.until(
    EC.element_to_be_clickable(
        (By.XPATH, "//button[@id='submit']")
    )
)
button.click()

Real Example: A submit button that’s grayed out until a form is filled. Only clickable when the form is complete.


4. 📝 Text-based Conditions

Question: “Does this element have the TEXT I’m looking for?”

Option A: Exact Text Present

# Wait for element with exact text
element = wait.until(
    EC.text_to_be_present_in_element(
        (By.ID, "status"),
        "Success!"
    )
)

Option B: Text in Element’s Value

# Wait for text in an input field
wait.until(
    EC.text_to_be_present_in_element_value(
        (By.ID, "searchBox"),
        "python"
    )
)

Real Example: After submitting a form, wait for “Thank you!” to appear.


Putting It All Together

Here’s a complete story of ordering pizza online:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 15)

# Go to pizza website
driver.get("https://pizzaplace.com")

# Wait for menu to be visible
wait.until(
    EC.visibility_of_element_located(
        (By.CLASS_NAME, "menu")
    )
)

# Wait for "Pepperoni" option to be clickable
pepperoni = wait.until(
    EC.element_to_be_clickable(
        (By.ID, "pepperoni-pizza")
    )
)
pepperoni.click()

# Wait for "Add to Cart" button
add_btn = wait.until(
    EC.element_to_be_clickable(
        (By.ID, "add-to-cart")
    )
)
add_btn.click()

# Wait for cart to show "1 item"
wait.until(
    EC.text_to_be_present_in_element(
        (By.ID, "cart-count"),
        "1"
    )
)

print("Pizza added successfully!")
driver.quit()

Quick Reference: When to Use What

Situation Use This Condition
Page loading, need to see content visibility_of_element_located
Check if hidden element exists presence_of_element_located
Need to click a button element_to_be_clickable
Waiting for message to appear text_to_be_present_in_element
Form field auto-fills text_to_be_present_in_element_value

Implicit vs Explicit: The Final Comparison

Feature Implicit Wait Explicit Wait
Set up Once, globally Per-element
Conditions Only “element exists” Many options!
Flexibility Low High
Best for Simple pages Complex interactions

Pro Tip: Many developers prefer explicit waits because they’re more precise. But for simple scripts, implicit waits work great!


Remember This! đź§ 

  1. Web pages are slow — Elements don’t appear instantly
  2. Implicit = Global patience — Set once, applies everywhere
  3. Explicit = Specific patience — Wait for exact conditions
  4. WebDriverWait — Your tool for explicit waits
  5. Conditions — Visibility, Presence, Clickability, Text

Think of yourself as a patient chef 👨‍🍳:

  • Don’t check if the cake is done every second (polling)
  • Don’t wait forever (set timeouts)
  • Check for the RIGHT thing (correct condition)
  • Celebrate when it’s ready! 🎉

Now you understand waits! You’ll never have flaky, crashing tests because of timing issues. Your Selenium scripts will be patient, reliable, and professional. ✨

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.