这个问题在这里已有答案:
我使用selenium来解析html,但是只能获取父文本。 html代码段如下:
<p>
<span>Who</span>
Mario
</p>
我得到了元素
div.find_element_by_xpath('p/span[contains(text/(), "Who")]../').text.strip()
但是这将返回p标签内的所有文本(“Who Mario”)。如何在没有.replace()等的情况下只检索父文本(Mario)?
试试这个:
div.find_element_by_xpath('p/span[contains(text(), "Who")]/following-sibling::text()').text.strip()