selenium - 使用selenium获取父文本[duplicate]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我使用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)?

python selenium xpath
1个回答
0
投票

试试这个:

div.find_element_by_xpath('p/span[contains(text(), "Who")]/following-sibling::text()').text.strip()
© www.soinside.com 2019 - 2024. All rights reserved.