How to Write XPath Using contains() for Partial Attribute Match

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.

What is the contains() Function in XPath?

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.

How to Use contains() for Partial Attribute Match

The syntax of the contains() function is as follows:

				
					contains(@attribute, 'substring')

				
			
  • @attribute: The attribute of the element you want to search for.
  • 'substring': The part of the attribute value you are looking for.

Example 1: Matching a class Attribute Partially

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:

				
					<button class="btn submit primary">Submit</button>

				
			

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.


Example 2: Matching a name Attribute Partially

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.

DOM Example:

				
					<input name="user_email" type="text" />

				
			

XPath Solution:

				
					//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.


Example 3: Matching a href Attribute Partially

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.

DOM Example:

				
					<a href="https://www.example.com/products">Products</a>

				
			

XPath Solution:

				
					//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.


Example 4: Matching a id Attribute Partially

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.

DOM Example:

				
					<input id="username_123" type="text" />

				
			

XPath Solution:

				
					//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.


Conclusion

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.

Related Post

How to Write XPath for Attribute-Based Elemen

XPath is a powerful tool for locating elements on a web...

How to Write XPath to Capture Elements by Exa

When automating web applications, you often need to loc...

Leave a Comment