Selenium Python -捕获包含特定文本的li元素

wbgh16ku  于 2023-06-06  发布在  Python
关注(0)|答案(1)|浏览(255)

我正在尝试从网页中提取urlToBeCaptured和要捕获的文本。结构如下所示:

<li>
 " text with trailing spaces "
<a href="urlToBeCaptured">
    <span class ="class1> Text to be captured </span>
    <span class ="class2> Another text </span>
</a>
...
</li>

我正在做以下工作,但似乎不起作用:

el = driver.find_element(By.XPATH, "//li[contains(text(),'text with trailing spaces')]")

一旦我找到了如何从class1中提取文本的元素,它应该是这样的吗?

textToBeCaptured = el.find_element(By.CLASS_NAME, 'class1').text
dba5bblo

dba5bblo1#

给定HTML:

<li>
    text with trailing spaces 
    <a href="urlToBeCaptured">
        <span class ="class1"> Text to be captured </span>
        <span class ="class2"> Another text </span>
    </a>
</li>

要定位带有text ”text with trailing spaces“ 的节点,然后它是带有class ="class1"的后代<span>,您可以使用以下locator strategies之一:

  • 使用 XPATHcontains()
print(driver.find_element(By.XPATH, "//li[contains(., 'text with trailing spaces')]/a//span[@class='class1']").text)
print(driver.find_element(By.XPATH, "//li[contains(normalize-space(),'text with trailing spaces')]/a//span[@class='class1']").text)

*注意:需要添加以下导入:

from selenium.webdriver.common.by import By

相关问题