Mugen
Mugen

Reputation: 1589

How can I point to an element that doesn't contain any HTML tag?

I have the following HTML code:

<html>

<span class='whatever'>

  <div @id='xyz'>
    "text1"
    "text2"   <=== I am trying to extract this text
  </div>
</span>
</html>

Is it possible to write an xpath that points to the node that is text2? If yes, then I can extract via .text (python).

Upvotes: 0

Views: 107

Answers (2)

JaSON
JaSON

Reputation: 4869

You can extract complete text with //div[@id='xyz']/text() XPath and then get required text with

text.split('\n')[-1]

Upvotes: 1

Kushal agrawal
Kushal agrawal

Reputation: 142

That really depends on what type of parser you are using for your html. Your html parser would provide you with something like a inner html or inner text node get module. You can use that and if you only want text2 you can use regular expressions or something other to filter the text out.

There is another method that if html is also written by you. Then you can enclose the text2 with span tag and directly get it.

Upvotes: 1

Related Questions