6

I am trying to figure out the XPath which will retrieve the 'text Data 3' from the following HTML snippet

  <span class="inner-span">
    Text-data 1
    <br>
    <span>Text Data 2</span> text Data 3
   </span>

So far i have tried the following Xpath which leads me to the span with class 'inner-span'

 /html/body/div/div[4]/div[2]/div[2]/div/div[2]/div/span[@class="inner-span"]

But dont know what to add more to Xpath which will only give me text 'text Data 3' from above Html snippet. Thanks

2 Answers 2

14

For your particular case this XPath query would work:

normalize-space(//span[@class="inner-span"]/text()[last()])

Tried it in this online tester and here's the result:

  1. Your HTML;
  2. My XPath expression;
  3. The expected result "text Data 3".

enter image description here

2
  • I checked your solution on the same link you provided but it gives all text-data from [@class="inner-span"]. I just want text data 3, not all text data 1,text data 2,text data 3.
    – shivamsupr
    Apr 27, 2013 at 7:11
  • I've added a screenshot to show you the result I got and how I got it, hope it helps. Apr 27, 2013 at 10:02
1

You could select /html/body/div/div[4]/div[2]/div[2]/div/div[2]/div/span[@class="inner-span"]/text()[last()] to get the text node (though with leading and trailing white space) or you can use an XPath returning a string with normalize-space(/html/body/div/div[4]/div[2]/div[2]/div/div[2]/div/span[@class="inner-span"]/text()[last()]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.