如何解决编写一个xpath来检查两个不同的html元素中存在的文本
我想写xpath来检查文本“一起购买两件物品可以节省$ 45.34”。但是正如您在下面看到的,它存在于不同的html标签中。我该怎么办?
<p _ngcontent-c41 class="black-font">
<strong _ngcontent-c41 class="ng-star-inserted">
"You can save "
<span -ngcontent-c41 class="dollar amount">$45.34</span>
" when you buy two items together."
</strong>
</p>
解决方法
要获取文字,您可以编写类似:
normalize-space(//p[@class="black-font"])
输出:
"You can save " $45.34 " when you buy two items together."
要检查Selenium中文本的存在,可以编写如下内容:
el = WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,"//strong[@_ngcontent-c41][contains(.,'You can save') and contains(.,'" when you buy two items together."')]")))
进口:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。