0
0
Selenium Pythontesting~15 mins

XPath with contains and starts-with in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login button using XPath with contains and starts-with
Preconditions (1)
Step 1: Locate the login button using XPath with contains() on the class attribute
Step 2: Verify the login button is visible and enabled
Step 3: Locate the login button using XPath with starts-with() on the id attribute
Step 4: Verify the login button text is 'Log In'
✅ Expected Result: The login button is found using both XPath expressions, is visible, enabled, and has the text 'Log In'
Automation Requirements - Selenium with Python
Assertions Needed:
Assert the login button is displayed
Assert the login button is enabled
Assert the login button text equals 'Log In'
Best Practices:
Use explicit waits to wait for elements to be visible
Use By.XPATH with contains() and starts-with() functions correctly
Avoid hardcoded sleeps
Use clear and maintainable locators
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

# Setup WebDriver (assuming chromedriver is in PATH)
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

try:
    # Open login page
    driver.get('https://example.com/login')

    # Locate login button using contains() on class attribute
    login_button_contains = wait.until(
        EC.visibility_of_element_located(
            (By.XPATH, "//button[contains(@class, 'login-btn')]")
        )
    )

    # Assert button is displayed and enabled
    assert login_button_contains.is_displayed(), "Login button (contains) is not displayed"
    assert login_button_contains.is_enabled(), "Login button (contains) is not enabled"

    # Locate login button using starts-with() on id attribute
    login_button_starts = wait.until(
        EC.visibility_of_element_located(
            (By.XPATH, "//button[starts-with(@id, 'loginBtn')]")
        )
    )

    # Assert button text is 'Log In'
    button_text = login_button_starts.text
    assert button_text == 'Log In', f"Expected button text 'Log In' but got '{button_text}'"

finally:
    driver.quit()

This script opens the login page and waits explicitly for the login button to appear using two XPath locators:

  • contains(@class, 'login-btn') finds a button whose class attribute includes 'login-btn'.
  • starts-with(@id, 'loginBtn') finds a button whose id attribute starts with 'loginBtn'.

We use WebDriverWait with visibility_of_element_located to wait until the button is visible, avoiding unreliable fixed waits.

Assertions check that the button is visible, enabled, and has the exact text 'Log In'.

Finally, the driver quits to close the browser.

Common Mistakes - 4 Pitfalls
Using hardcoded sleep instead of explicit waits
Using absolute XPath instead of relative XPath with contains/starts-with
Not verifying element visibility before interacting
{'mistake': 'Using incorrect XPath syntax for contains or starts-with', 'why_bad': 'Incorrect syntax causes locator failures and test errors.', 'correct_approach': "Use correct XPath functions: contains(@attribute, 'value') and starts-with(@attribute, 'value')."}
Bonus Challenge

Now add data-driven testing with 3 different login button class names and id prefixes to verify the button presence and text for each case.

Show Hint