silhouette hustler
silhouette hustler

Reputation: 1763

Locate parent element that contains specific child element

What I would like to do is to locate all the parent elements that contain a specific child.

<tr>
 <i class="myClass"> </i>
</tr>

So in this case I would like to locate all tr tags that contain i tag with this specific class.

Is there any possible way of doing this?

Upvotes: 2

Views: 3869

Answers (3)

kjhughes
kjhughes

Reputation: 111491

Other answers thus far are more complicated than necessary. This simple XPath will select the tr elements with child i elements with a class attribute equal to myClass:

//tr[i/@class='myClass']

If i was just an example, and really the goal is to select tr elements with any child element with the given @class attribute value, then this'll work instead:

//tr[*/@class='myClass']

Upvotes: 2

Drew
Drew

Reputation: 66

Here's a way using XPath.

//*[@class='myClass']/parent::tr

Upvotes: 2

murali selenium
murali selenium

Reputation: 3927

You can use parent axis in xpath

//i[class="myClass"]/parent::tr

The above one selects parent elements having tag tr. To select all parents

//i[class="myClass"]/parent::*

please find xpath axes here

Thank You, Murali

Upvotes: 2

Related Questions