0
0
Selenium Pythontesting~10 mins

Custom wait conditions in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses a custom wait condition to wait until a specific element's text changes to the expected value before proceeding. It verifies that the text update happens within the timeout.

Test Code - Selenium
Selenium Python
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
from selenium.common.exceptions import TimeoutException

class text_to_be_updated:
    def __init__(self, locator, text):
        self.locator = locator
        self.text = text

    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        if element.text == self.text:
            return element
        else:
            return False


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

    locator = (By.ID, "status")
    wait = WebDriverWait(driver, 10)

    try:
        element = wait.until(text_to_be_updated(locator, "Complete"))
        assert element.text == "Complete"
    except TimeoutException:
        assert False, "Text did not update to 'Complete' within 10 seconds"
    finally:
        driver.quit()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open with no page loaded-PASS
2Browser navigates to https://example.com/dynamic_textPage with element id='status' is loading dynamic text-PASS
3WebDriverWait starts waiting up to 10 seconds for element with id='status' text to become 'Complete'Element with id='status' initially shows different text (e.g., 'Loading...')Check if element.text == 'Complete'PASS
4Custom wait condition __call__ method repeatedly checks element textElement text changes from 'Loading...' to 'Complete' within timeoutElement text equals 'Complete'PASS
5Assertion verifies element.text is 'Complete'Element text is 'Complete' as expectedassert element.text == 'Complete'PASS
6Browser closes and test endsBrowser window closed-PASS
Failure Scenario
Failing Condition: Element text does not change to 'Complete' within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
What does the custom wait condition check for?
AElement is visible
BElement is clickable
CElement's text equals 'Complete'
DPage title contains 'Complete'
Key Result
Using custom wait conditions lets you wait for very specific states, like text changes, improving test reliability for dynamic content.