编写一个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

最新更新