Or copy link
In Selenium automation, XPath is a powerful method for locating web elements. Sometimes, you may not have the exact value of an element’s attribute, but you may know part of the value. This is where the contains() function in XPath comes into play. The contains() function allows you to match a part of an attribute’s value, making it more flexible and useful when dealing with dynamic or partially known attributes.
contains()
The contains() function in XPath is used to check if an attribute value contains a specific substring. Instead of requiring the complete attribute value to match exactly, contains() allows you to search for elements whose attribute values include a particular text. This function is particularly useful when dealing with dynamic web pages where attribute values change frequently but contain a known part.
The syntax of the contains() function is as follows:
contains(@attribute, 'substring')
@attribute
'substring'
class
In many cases, elements have multiple classes or dynamically generated class names. Instead of trying to match the entire class attribute value, you can use contains() to match part of the class name.
DOM Example:
Submit
XPath Solution:
//button[contains(@class, 'submit')]
Explanation: The XPath expression //button[contains(@class, 'submit')] will select the button element whose class attribute contains the substring “submit”. This is useful when the exact class name is unknown, but you can rely on part of it.
button
name
Sometimes, the name attribute of form elements contains dynamic values or additional text that you may not need to match fully. Using contains(), you can capture elements based on a partial match.
//input[contains(@name, 'user')]
Explanation: The XPath //input[contains(@name, 'user')] selects the input element whose name attribute contains the substring “user”. This approach works even if the name attribute has additional characters or dynamic parts.
input
href
The href attribute of links often contains long URLs, but you may only need to match part of the URL, such as a specific domain or path. The contains() function is perfect for this.
Products
//a[contains(@href, 'example.com')]
Explanation: The XPath //a[contains(@href, 'example.com')] selects the a (anchor) element whose href attribute contains the substring “example.com”. This allows you to match the link even if the full URL or query parameters change dynamically.
a
id
The id attribute is often dynamically generated, so it may include random characters or timestamps. Using contains(), you can target elements based on part of the id value.
//input[contains(@id, 'username')]
Explanation: The XPath //input[contains(@id, 'username')] selects the input element whose id attribute contains the substring “username”. This technique is helpful when the id is dynamic, but the initial part remains consistent.
The contains() function in XPath is a versatile tool for locating elements when you only know part of an attribute’s value. By using this function, you can write more flexible and robust XPath expressions, especially in scenarios where element attributes are dynamic or only partially known.
By mastering the contains() function, you can handle a wide range of situations in web automation, ensuring that your tests remain maintainable and adaptable to changes in the DOM.
When automating web applications, you often need to loc...
XPath is a powerful tool for locating elements on a web...
Save my name, email, and website in this browser for the next time I comment.
Δ