Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I match on an attribute that contains a certain string?

Tags:

xpath

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>?

like image 295
crazyrails Avatar asked Sep 07 '09 18:09

crazyrails


People also ask

How do you locate an element by partially comparing its attributes in XPath?

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.

What is contain in XPath?

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.

How do I start with XPath?

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.


1 Answers

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.

like image 194
surupa123 Avatar answered Sep 20 '22 14:09

surupa123