Selenium Locators in Python: Unlocking the Power of contains, starts-with, and, or

|
| By Gourav Singh

When working with Selenium WebDriver, selecting the right element from a web page is crucial for successful automation. Whether it’s clicking a button, entering text into a form, or verifying page content, locators are the key. Selenium provides various strategies to locate web elements, but sometimes static locators like id, class, or name are not sufficient.

This is where advanced XPath methods, such as contains() and starts-with(), along with operators like and and or, make testing web elements easier. This blog will explore these methods and how to use them effectively in Selenium with Python.

Locators in Selenium

Selenium provides different types of locators to find elements:

  • ID: driver.find_element(By.ID, “element_id”)
  • Name: driver.find_element(By.NAME, “element_name”)
  • Class Name: driver.find_element(By.CLASS_NAME, “class_name”)
  • Tag Name: driver.find_element(By.TAG_NAME, “tag_name”)
  • Link Text: driver.find_element(By.LINK_TEXT, “link text”)
  • Partial Link Text: driver.find_element(By.PARTIAL_LINK_TEXT, “partial text”)
  • XPath: driver.find_element(By.XPATH, “xpath”)

Among all, XPath is one of the most powerful and flexible locator strategies.

Why Use XPath?

XPath (XML Path Language) allows us to navigate through elements and attributes in an XML document (or HTML). It can be used to locate elements dynamically using conditions, functions, and expressions.

Sometimes, the attributes like id or class are dynamic or partially known. XPath allows handling such situations using functions like:

  • contains()
  • starts-with()
  • text()
  • Logical conditions like and, or
Using contains() in XPath

The contains() method is helpful when an attribute value is partially known or dynamic.

Syntax:

//tagname[contains(@attribute, ‘value’)]

Example:

driver.find_element(By.XPATH, “//input[contains(@name, ’email’)]”)

This would match any input element whose name attribute contains the word “email”, such as user_email, email_id, etc.

Real-world use:

On many websites, input fields or buttons have dynamic names like:

<input name=”user_email_1234″ type=”text” />

Using:

driver.find_element(By.XPATH, “//input[contains(@name, ’email’)]”)

It is better than trying to match the exact attribute value.

Using starts-with() in XPath

starts-with() is used when the starting part of an attribute is known.

Syntax:

//tagname[starts-with(@attribute, ‘value’)]

Example:

driver.find_element(By.XPATH, “//div[starts-with(@class, ‘alert-‘)]”)

This XPath matches any div whose class starts with alert-, such as alert-warning, alert-success, etc.

Using Logical Operators: and / or

XPath allows combining multiple conditions using and and or.

Syntax with and:

//tagname[@attribute1=’value1′ and @attribute2=’value2′]

Example:

driver.find_element(By.XPATH, “//input[@type=’text’ and @name=’username’]”)
Matches input fields where both conditions are true.

Syntax with or:

//tagname[@attribute1=’value1′ or @attribute2=’value2′]

Example:

driver.find_element(By.XPATH, “//button[@id=’submit’ or @name=’submit_button’]”)

Matches buttons with either id=submit or name=submit_button.

Combining Multiple Methods

We can also combine contains(), starts-with(), and logical operators.

Example 1 – Using contains() + and:

driver.find_element(By.XPATH, “//a[contains(@href, ‘login’) and contains(text(), ‘Sign’)]”)
This matches anchor tags that contain login in the URL and the word Sign in the text.

Example 2 – Using starts-with() + or:

driver.find_element(By.XPATH, “//span[starts-with(@class, ‘icon-‘) or @id=’iconMenu’]”)

Example 3 – Tag independent:

driver.find_element(By.XPATH, “//*[contains(text(), ‘Welcome’)]”)
Matches any element that contains the word “Welcome”.

Selenium + XPath in Python: Real Example

Here’s a short, working code example that ties everything together to sign in to https://winsurtechglow.com/signin and verify the page title. I’ve used a dummy username and password for demonstration.

import time
from selenium import webdriver
from selenium.webdriver.common.by import Bydriver = webdriver.Chrome()
driver.get(“https://winsurtechglow.com/signin”)# Using contains()
driver.find_element(By.XPATH, “//*[contains(@name, ‘user’)]”).send_keys(“*****.singh@webners.com”)

# Using starts-with()
driver.find_element(By.XPATH, “//*[starts-with(@name, ‘Pass’)]”).send_keys(“**password***”)

# Using and
driver.find_element(By.XPATH, “//input[@id=’submitbtn’ and @value=’Sign In’]”).click()

actual_title=driver.title
expected_title=”FormCruise Business Dashboard | CRM for Insurance Agencies.”

if actual_title==expected_title:
    print(“\”title is matching\””)

else:
    print(“\”title is not matching\””)

# Wait for 1 minute before closing the browser
time.sleep(10)

# Close the browser
driver.quit()

Best Practices for XPath
  • Prefer short, readable, and robust XPaths.
  • Avoid using absolute XPath like /html/body/div/… — they are brittle.
  • Use contains() or starts-with() wisely; don’t overuse them.
  • Use normalize-space(text()) to ignore leading/trailing spaces in text.
Bad XPath:

driver.find_element(By.XPATH, “/html/body/div[2]/div[1]/form/input[3]”)

Good XPath:

driver.find_element(By.XPATH, “//input[contains(@name, ’email’)]”)

When to Use These XPath Techniques?
Scenario Technique
Dynamic attribute value contains()
Known prefix of an attribute starts-with()
Multiple conditions to match and, or
Text-based matching contains(text(), ‘value’) or text()=’value’
Fallback for dynamic elements Combine contains, or, and
Conclusion

In Selenium automation, mastering XPath is like learning to wield a sharp sword—it lets us to slice through complex HTML structures with precision.

Functions like contains(), starts-with(), and logical operators (and, or) empower you to locate elements even when the structure is dynamic, unpredictable, or poorly designed.

By practicing these advanced XPath techniques in our Selenium tests, we can build robust, flexible, and future-proof test scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *