Saturday, October 13, 2018

XPath Using 'contains()'

When an attribute of an element is dynamic then one can use 'contains()' to select the element by using the constant part of the attribute of that web element.

Syntax: 
//tag[contains(@attribute, 'value')]
Example:
XPath = //tag[contains(@name, 'btnClk')]
It searches 'btnClk' for all name attributes in the DOM.
XPath = //tag[contains(text(), 'here')]
It searches the text 'here' in the DOM.
XPath = //tag[contains(@href, 'google.com')]
It searches 'google.com' link in the DOM

Let's get a clear view with the below example:
//span[contains(@class, 'gb_')]
XPath Using 'contains()'

Here, //span[contains(@class, 'gb_')] searches all the 'span' elements which contains 'gb_' as attribute class value.

Another example:
//a[contains(text(), 'ac')]
'contains()' & 'text()' function in XPath

Here, //a[contains(text(), 'ac')] searches all the 'a' elements which contains 'ac' as attribute text value.

No comments:

Post a Comment