简体   繁体   English

Selenium:如何按文本查找元素

[英]Selenium: how to find element by text

I'm trying to search element by text with selenium but didn't find anything that work.我正在尝试使用 selenium 按文本搜索元素,但没有找到任何有效的方法。 The element is displayed below:该元素如下所示:

<div class="slick-cell l0 r0 project"><span class="project-name"><a href="/reports.htm#p10075">MFIT</a></span></div>

There are multiple elements of this type so I need to find this element by MFIT and return the href.这种类型有多个元素,所以我需要通过MFIT找到这个元素并返回 href。

I've tried multiples possibilities using XPath which didn't work.我使用 XPath 尝试了多种可能性,但都不起作用。

  • driver.find_elements(By.XPATH, "//*[contains(@*, 'MFIT')]") and go over the list driver.find_elements(By.XPATH, "//*[contains(@*, 'MFIT')]")并遍历列表
  • driver.find_element(By.XPATH, "//*[contains(@*, 'MFIT')]")
  • driver.find_element(By.XPATH, "//span[@class='project-name' and contains(., 'MFIT')]")

Can someone help me ?有人能帮我吗 ?

Try:尝试:

driver.find_element(By.XPATH, "//a[contains(text(), 'MFIT')]").get_attribute('href')


WebDriverWait(driver,20).untill(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(), 'MFIT')]")))

#imports #imports

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM