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?

oste-popp
  • 190
  • 1
  • 10
  • 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 https://stackoverflow.com/questions/45303767/get-some-text-with-java-selenium-webdriver/45318212#45318212 – NarendraR Jan 13 '18 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)` – yong Jan 14 '18 at 04:35

1 Answers1

0

Try this one:

div.find_element_by_xpath('p/span[contains(text(), "Who")]/following-sibling::text()').text.strip()
johnII
  • 1,423
  • 1
  • 14
  • 20