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?
//p/text()['Mario']/preceding-sibling::span. But selenium doesn't allow you to locate an element using text node. for solution you have to useexecute_scriptin python to execute your xpath query and then get the result. Refer this for more stackoverflow.com/questions/45303767/…ele_p=driver.finde_element_by_xpath('//p[contains(. ,"Who")]');print driver.execute_script('return arguments[0].lastChild.textContent;', ele_p)