1

I have following code

<div>
  <p>some paragraph</p>
   some nasty   text that I need
<span>something else</span>
</div>

Now I need to get some nasty text that I need only. How to do it using only XPath 1.0? Is it possible?

master.py
  • 215
  • 2
  • 5
  • 16
  • It's not possible as per this answer. [http://stackoverflow.com/a/21651144/4193730](http://stackoverflow.com/a/21651144/4193730) – Subh Nov 19 '14 at 07:16

1 Answers1

0

How to do it using only XPath 1.0? Is it possible?

Yes - and it's rather trivial:

/div/text()

I wonder why you did not try that? All other text nodes are either in a p or span element and should not cause you any trouble.

Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
  • Yes, I tried, but this gives me an error in Selenium - I don't know why? So I thought there is a different solution. – master.py Nov 19 '14 at 03:48
  • In Selenium, you have to use "/div" as XPATH and then you need to call ".getText()" (Java) method to get the text content. But note that it will give you entire text under "div" tag, that is, including "p" and "span" tag's text content. I don't think, any option to get only text that you want "some nasty text that I need". If you want text in child tag only then we can get it using XPATH "/div/span". – Surya Nov 19 '14 at 06:13
  • @Surya Thanks, that clears it up. Until the question was edited, we did not know it was Selenium. I'm rather unfamiliar with Selenium, so I cannot comment on a solution there. – Mathias Müller Nov 19 '14 at 10:05