0
0
Selenium Pythontesting~5 mins

XPath with contains and starts-with in Selenium Python

Choose your learning style9 modes available
Introduction

XPath with contains and starts-with helps find elements when you only know part of their attribute or text. This makes tests more flexible and less fragile.

When the exact attribute value changes but has a common part.
When you want to find buttons or links starting with certain text.
When elements have dynamic IDs but share a prefix.
When you want to locate elements by partial text content.
When you want to avoid brittle tests that break on small UI changes.
Syntax
Selenium Python
//*[contains(@attribute, 'value')]
//*[starts-with(@attribute, 'value')]

contains() checks if the attribute or text has the given substring anywhere.

starts-with() checks if the attribute or text begins exactly with the given substring.

Examples
Finds any <button> element whose id attribute contains 'submit' anywhere.
Selenium Python
//button[contains(@id, 'submit')]
Finds any <a> link element whose visible text starts with 'Log'.
Selenium Python
//a[starts-with(text(), 'Log')]
Finds any element with a class attribute containing 'active'.
Selenium Python
//*[contains(@class, 'active')]
Finds any <input> element whose name attribute starts with 'user'.
Selenium Python
//input[starts-with(@name, 'user')]
Sample Program

This script opens a simple HTML page with buttons and links. It uses XPath with contains and starts-with to find elements by partial attribute values and text. It prints the found elements' text or tag name to show the correct elements were found.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

# Setup WebDriver (make sure chromedriver is in PATH)
options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)

# Open a simple test page with buttons
html = '''
<html>
  <body>
    <button id="btn_submit_123">Submit</button>
    <button id="btn_cancel_456">Cancel</button>
    <a href="#" class="link active">Login</a>
    <a href="#" class="link">Logout</a>
  </body>
</html>
'''

# Use data URL to load HTML
driver.get("data:text/html;charset=utf-8," + html)

# Find button with id containing 'submit'
submit_button = driver.find_element(By.XPATH, "//button[contains(@id, 'submit')]")
print(submit_button.text)  # Should print 'Submit'

# Find link starting with text 'Log'
link = driver.find_element(By.XPATH, "//a[starts-with(text(), 'Log')]")
print(link.text)  # Should print 'Login'

# Find element with class containing 'active'
active_link = driver.find_element(By.XPATH, "//*[contains(@class, 'active')]")
print(active_link.tag_name)  # Should print 'a'

# Find button starting with id 'btn_cancel'
cancel_button = driver.find_element(By.XPATH, "//button[starts-with(@id, 'btn_cancel')]")
print(cancel_button.text)  # Should print 'Cancel'

# Close driver
driver.quit()
OutputSuccess
Important Notes

Always prefer unique and stable attributes for locating elements.

Using contains and starts-with helps when attributes change dynamically.

Test your XPath expressions in browser DevTools before using them in code.

Summary

contains() finds elements with attribute or text containing a substring.

starts-with() finds elements with attribute or text starting with a substring.

These XPath functions make locating elements more flexible and reliable.