I am having a problem selecting nodes by attribute when the attributes contains more than one word. For example:
<div class="atag btag" />
This is my xpath expression:
//*[@class='atag']
The expression works with
<div class="atag" />
but not for the previous example. How can I select the <div>
?
We can identify elements by partially comparing to its attributes in Selenium with the help of regular expression. In xpath, there is contains () method. It supports partial matching with the value of the attributes. This method comes as useful while dealing with elements having dynamic values in their attributes.
contains() in Selenium is a function within Xpath expression which is used to search for the web elements that contain a particular text. We can extract all the elements that match the given text value using the XPath contains() function throughout the webpage.
XPath starts-with() is a function used for finding the web element whose attribute value gets changed on refresh or by other dynamic operations on the webpage. In this method, the starting text of the attribute is matched to find the element whose attribute value changes dynamically.
Here's an example that finds div elements whose className contains atag
:
//div[contains(@class, 'atag')]
Here's an example that finds div elements whose className contains atag
and btag
:
//div[contains(@class, 'atag') and contains(@class ,'btag')]
However, it will also find partial matches like class="catag bobtag"
.
If you don't want partial matches, see bobince's answer below.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With