Introduction
XPath Functions are special functions provided by XPath that allow Selenium to locate web elements based on their attributes, visible text, or normalized text values. They provide flexible and powerful ways to identify elements that cannot be reliably located using simple XPath expressions.
Modern web applications frequently contain dynamic content, extra whitespace, and partially changing attribute values. XPath Functions help automation engineers create maintainable and reliable locators for such scenarios.
In this tutorial, you’ll learn what XPath Functions are, why they are used, commonly used XPath functions, practical examples, real-world use cases, common mistakes, and best practices.
What are XPath Functions?
XPath Functions are predefined functions used within XPath expressions to locate web elements more efficiently.
They allow Selenium to:
Locate elements using visible text.
Perform partial text matching.
Ignore unnecessary whitespace.
Handle dynamic web elements.
Improve locator flexibility and maintainability.
For example, consider the following HTML:
<h1>Welcome to the Internet</h1>
<a>
Form Authentication
</a>
The elements can be located using XPath Functions such as:
//h1[contains(text(),'Welcome')]
and
//a[normalize-space()='Form Authentication']
XPath Functions significantly improve the reliability of Selenium locators when working with modern web applications.
Why Use XPath Functions?
XPath Functions are useful because they:
Support locating dynamic web elements.
Perform partial text matching.
Handle extra spaces in visible text.
Improve locator maintainability.
Produce flexible XPath expressions.
Are widely used in professional Selenium automation frameworks.
Syntax
Using contains()
driver.find_element(
By.XPATH,
"//tagname[contains(text(),'value')]"
)
Using text()
driver.find_element(
By.XPATH,
"//tagname[text()='value']"
)
Using normalize-space()
driver.find_element(
By.XPATH,
"//tagname[normalize-space()='value']"
)
Commonly Used XPath Functions
1. contains()
Locates elements whose visible text or attribute values contain specific text.
//h1[contains(text(),'Welcome')]
2. text()
Locates elements using their exact visible text.
//button[text()='Login']
3. normalize-space()
Removes unnecessary leading and trailing spaces before performing text comparisons.
//a[normalize-space()='Form Authentication']
These functions are widely used when working with dynamic and text-based web elements.
Example
The Selenium practice website contains a heading and a hyperlink that can be located efficiently using XPath Functions.
The Selenium code is:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 13. XPath - XPath Functions
# Practice site: https://the-internet.herokuapp.com/
# Run: pytest -s 13_examples/test_05_xpath_functions.py
#
# XPath functions like text(), contains(), and normalize-space() help match
# elements based on their text content.
def test_xpath_functions():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/")
heading = driver.find_element(
By.XPATH,
"//h1[contains(text(), 'Welcome')]"
)
link = driver.find_element(
By.XPATH,
"//a[normalize-space()='Form Authentication']"
)
assert "Welcome" in heading.text
assert link.is_displayed()
finally:
driver.quit()
Output
The webpage heading and
Form Authentication hyperlink
are located successfully using
XPath Functions.
Understanding the Code
Import the By Class
from selenium.webdriver.common.by import By
Imports Selenium’s locator strategies.
Open the Practice Website
driver.get(
"https://the-internet.herokuapp.com/"
)
Launches the Selenium practice website.
Locate the Heading
driver.find_element(
By.XPATH,
"//h1[contains(text(),'Welcome')]"
)
Uses the contains() function to locate the webpage heading using partial visible text.
Locate the Hyperlink
driver.find_element(
By.XPATH,
"//a[normalize-space()='Form Authentication']"
)
Uses the normalize-space() function to remove unnecessary whitespace before locating the hyperlink.
Validate the Elements
assert "Welcome" in heading.text
assert link.is_displayed()
Verifies that the heading contains the expected text and confirms that the hyperlink is visible on the webpage.
How XPath Functions Work
Python Script
│
▼
By.XPATH
│
▼
Apply XPath Function
│
▼
Process Text or Attribute
│
▼
Locate Matching Element
│
▼
Perform Validation
Practical Example
Suppose you’re automating a webpage that displays dynamic headings.
Instead of locating the entire visible text, Selenium uses:
//h1[contains(text(),'Welcome')]
to locate the heading successfully even if the remaining text changes during future releases.
This significantly improves locator flexibility and maintainability.
Automation Testing Example
Consider an online banking application.
The Login hyperlink contains unnecessary spaces that differ slightly across environments.
Instead of using:
//a[text()='Login']
Selenium uses:
//a[normalize-space()='Login']
to reliably locate the hyperlink during regression testing.
Real-World Example
Automation engineers frequently use XPath Functions while working with:
Banking applications.
E-Commerce websites.
Healthcare portals.
CRM systems.
ERP applications.
SaaS products.
Enterprise web applications.
Dynamic dashboards.
XPath Functions are particularly useful whenever applications contain partially changing text values or unnecessary whitespace.
Advantages of XPath Functions
Support locating dynamic web elements.
Handle partially changing text values.
Ignore unnecessary whitespace.
Improve locator maintainability.
Produce flexible XPath expressions.
Widely used in professional Selenium automation frameworks.
Limitations of XPath Functions
Complex XPath expressions may reduce readability.
Incorrect partial matching can return unintended elements.
Overusing XPath Functions can make automation scripts difficult to maintain.
Poorly written expressions may affect locator reliability.
Common Mistakes Beginners Make
Using Exact Text Matching Unnecessarily
Avoid
//h1[text()='Welcome to the Internet']
Prefer
//h1[contains(text(),'Welcome')]
Partial text matching often produces more maintainable locators.
Ignoring Extra Spaces
If webpages contain unnecessary whitespace, use:
normalize-space()
instead of relying solely upon exact text matching.
Writing Overly Complex XPath Expressions
Keep XPath expressions simple and readable whenever possible.
Best Practices
Use
contains()whenever partial text matching improves reliability.Use
normalize-space()for elements containing unnecessary whitespace.Keep XPath expressions short and maintainable.
Verify XPath expressions using browser Developer Tools.
Avoid unnecessarily complex XPath Functions.
Write reusable XPath locators within automation frameworks.
Prefer stable locator strategies whenever possible.
Conclusion
XPath Functions provide flexible and maintainable solutions for locating dynamic web elements using their visible text and normalized values. They significantly improve the reliability of Selenium automation scripts when working with modern web applications.
Understanding XPath Functions provides an excellent foundation for learning XPath Axes and other advanced XPath techniques used extensively in professional Selenium automation frameworks.
Frequently Asked Questions (FAQs)
What are XPath Functions?
XPath Functions are predefined functions used within XPath expressions to locate web elements using their text values or attributes.
Which XPath Functions are commonly used in Selenium?
The most commonly used XPath Functions include:
contains()text()normalize-space()
Why is contains() useful?
It allows Selenium to locate elements using partial text matching, making XPath expressions more flexible.
When should I use normalize-space()?
Use normalize-space() whenever webpages contain unnecessary leading or trailing spaces within their visible text.
Are XPath Functions useful for dynamic web elements?
Yes.
XPath Functions are widely used for locating dynamic web elements whose visible text or attributes change partially during execution.
Key Takeaways
XPath Functions provide flexible techniques for locating dynamic web elements.
contains(),text(), andnormalize-space()are the most commonly used XPath Functions.They improve locator maintainability and framework reliability.
Use partial text matching whenever appropriate.
Keep XPath expressions simple, readable, and reusable.
XPath Functions are widely used in professional Selenium automation frameworks.
