0

I use selenium to parse a html, but have trouble getting only the parent text. The html snippet looks like:

<p>
  <span>Who</span>
  Mario
</p>

I get the element with

div.find_element_by_xpath('p/span[contains(text/(), "Who")]../').text.strip()

But this returns all text ("Who Mario") inside the p tag. How can i only retrieve the parent text (Mario) without .replace(), etc?

2
  • 1
    Use this xpath //p/text()['Mario']/preceding-sibling::span . But selenium doesn't allow you to locate an element using text node. for solution you have to use execute_script in python to execute your xpath query and then get the result. Refer this for more stackoverflow.com/questions/45303767/… Commented Jan 13, 2018 at 13:16
  • NarendraR is very correct, if you want to directly get that text you have to use execute_script, please try ele_p=driver.finde_element_by_xpath('//p[contains(. ,"Who")]');print driver.execute_script('return arguments[0].lastChild.textContent;', ele_p) Commented Jan 14, 2018 at 4:35

1 Answer 1

0

Try this one:

div.find_element_by_xpath('p/span[contains(text(), "Who")]/following-sibling::text()').text.strip()
Sign up to request clarification or add additional context in comments.

1 Comment

Hm, I get invalid selector is: [object Text]. It should be an element

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.