如何找到其中包含特定文本的元素?
How can I locate an element with specific text in it?
label
是我要查找的元素,我知道的文本是Cost of paving parking lot in front of building
。
我尝试了 contains
、innerText()
和 textContent()
以下 xpath 应匹配所有 label
元素,这些元素的后代节点带有包含您的文本的标记 p
。试试它是否能解决您的问题。
label_elem = driver.find_element_by_xpath("//label[.//p[contains(text, 'Cost of paving parking lot in front of building.')]]")
根据包含文本 Cost of paving parking lot in front of building 的 <p>
元素定位 您可以使用以下任一项 :
针对 <p>
标签使用 XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//p[contains(., 'Cost of paving parking lot in front of building')]//ancestor::label[@class='ahe-ui-radio']")))
对子标签使用 XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//label[@class='ahe-ui-radio' and @for][.//p[contains(., 'Cost of paving parking lot in front of building')]]")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
label
是我要查找的元素,我知道的文本是Cost of paving parking lot in front of building
。
我尝试了 contains
、innerText()
和 textContent()
以下 xpath 应匹配所有 label
元素,这些元素的后代节点带有包含您的文本的标记 p
。试试它是否能解决您的问题。
label_elem = driver.find_element_by_xpath("//label[.//p[contains(text, 'Cost of paving parking lot in front of building.')]]")
根据包含文本 Cost of paving parking lot in front of building 的 <p>
元素定位 您可以使用以下任一项
针对
<p>
标签使用 XPATH:element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//p[contains(., 'Cost of paving parking lot in front of building')]//ancestor::label[@class='ahe-ui-radio']")))
对子标签使用 XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//label[@class='ahe-ui-radio' and @for][.//p[contains(., 'Cost of paving parking lot in front of building')]]")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC