Python和Selenium-获取不包含子节点文本的文本 [英] Python and Selenium - get text excluding child node's text
本文介绍了Python和Selenium-获取不包含子节点文本的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Python 3 .
假设:
<whatever>
text
<subchild>
other
</subchild>
</whatever>
如果我愿意:
elem = driver.find_element_by_xpath("//whatever")
elem.text包含其他文本"
elem.text contains "text other"
如果我愿意:
elem = driver.find_element_by_xpath("//whatever/text()[normalize-space()]")
元素不是Webelement.
elem is not Webelement.
我如何继续仅抓取文本"(而不抓取其他")?
How my I proceed to grab only "text" (and not "other")?
目标:仅在直接节点中捕获文本,而不在子节点中捕获
Id est: grab only text in direct node, not the child nodes.
更新:
原始HTML是:
<div class="border-ashes the-code text-center">
VIVEGRPN
<span class="cursor"></span>
<button class="btn btn-ashes zclip" data-clipboard-target=".the-code" data-coupon-code="VklWRUdSUE4=">
<span class="r">Hen, la.</span>
</div>
推荐答案
您可以从所有文本中删除子节点文本
You can remove the child node text from the all text
all_text = driver.find_element_by_xpath("//whatever").text
child_text = driver.find_element_by_xpath("//subchild").text
parent_text = all_text.replace(child_text, '')
这篇关于Python和Selenium-获取不包含子节点文本的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文