Zihao Zhao
Zihao Zhao

Reputation: 499

Get the element with text and its parent doesn't have text

<div class="a">
  <div>Histogram
    <em>Median</em>
  </div>
  <div>
    <em>Median</em>
  </div>
</div>

In this case, how to get the second node contains 'Median' using xpath?

//div[@class="a"]//*[text()="Median"] will give two nodes.

Upvotes: 0

Views: 49

Answers (3)

KunduK
KunduK

Reputation: 33384

Try the below xpath.

(//div[@class='a']//em[text()='Median'])[last()]

Upvotes: 0

Ed Bangga
Ed Bangga

Reputation: 13006

you can use following-sibling

//div[@class="a"]/div[em[text()="Median"]]/following-sibling::div/em

Upvotes: 1

Jack Fleeting
Jack Fleeting

Reputation: 24930

Try

(/div[@class="a"]//*[text()="Median"])[2]

Upvotes: 0

Related Questions