0
0
Selenium Pythontesting~15 mins

Custom expected conditions in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Wait for a specific text to appear in an element using a custom expected condition
Preconditions (2)
Step 1: Navigate to the target web page
Step 2: Locate the element by its ID 'status-message'
Step 3: Wait up to 10 seconds for the element's text to become exactly 'Completed'
Step 4: Verify that the element's text is 'Completed'
✅ Expected Result: The test waits until the element with ID 'status-message' shows the text 'Completed' and then confirms the text matches exactly
Automation Requirements - Selenium with Python
Assertions Needed:
Assert the element's text is exactly 'Completed' after waiting
Best Practices:
Use WebDriverWait with a custom expected condition class
Use explicit waits instead of implicit waits or sleep
Use By.ID locator for element identification
Handle exceptions gracefully
Automated Solution
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 TextEquals:
    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_wait_for_text():
    driver = webdriver.Chrome()
    try:
        driver.get('https://example.com/dynamic-text')
        wait = WebDriverWait(driver, 10)
        element = wait.until(TextEquals((By.ID, 'status-message'), 'Completed'))
        assert element.text == 'Completed', f"Expected text 'Completed' but got '{element.text}'"
    except TimeoutException:
        assert False, "Timed out waiting for text 'Completed' in element with ID 'status-message'"
    finally:
        driver.quit()

This script defines a custom expected condition class TextEquals that waits for an element's text to match exactly a given string.

The __call__ method is used by Selenium's WebDriverWait to repeatedly check the element's text until it matches or timeout occurs.

The test function test_wait_for_text opens the browser, navigates to the page, and waits up to 10 seconds for the text 'Completed' to appear in the element with ID 'status-message'.

If the text matches, it asserts success; if it times out, it asserts failure with a clear message.

Finally, the browser is closed to clean up resources.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Hardcoding XPath locators that are brittle
Not handling TimeoutException
Returning true instead of the element in custom expected condition
Bonus Challenge

Now add data-driven testing with 3 different expected texts: 'Completed', 'Failed', and 'Pending'.

Show Hint