Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath - Get parent class by matching two child nodes

Tags:

xpath

I'd like to use xpath to select a link whose class="watchListItem", span="icon icon_checked", and h3="a test". I can use xpath to get either matching link and span, or link and h3, but not link, span, and h3.

Here's what I've tried:

//*[@class = 'watchListItem']/span[@class = 'icon icon_checked']

//*[@class= 'watchListItem']/h3[text()='AA']

I'm looking for something like this:

//*[@class = 'watchListItem']//*[span[@class = 'icon icon_checked'] and h3[text()='AA']]

<li>
<a class="watchListItem" data-id="thisid1" href="javascript:void(0);">
<span class="icon icon_checked"/>
<h3 class="itemList_heading">a test</h3>
</a>
</li>

<li>
<a class="watchListItem" data-id="thisid2" href="javascript:void(0);">
<span class="icon icon_unchecked"/>
<h3 class="itemList_heading">another test</h3>
</a>
</li>

<li>
<a class="watchListItem" data-id="thisid3" href="javascript:void(0);">
<span class="icon icon_checked"/>
<h3 class="itemList_heading">yet another test</h3>
</a>
</li>
like image 868
jossetter Avatar asked Mar 28 '13 15:03

jossetter


Video Answer


1 Answers

You can use the child:: location paths like so:

//a[@class="watchListItem"
    and child::span[@class="icon icon_checked"]
    and child::h3[text()="another test"]]

This would select the anchor with data-id="thisid3".

like image 61
Ja͢ck Avatar answered Oct 22 '22 11:10

Ja͢ck