Home > other >  Selenium: how to find element by text
Selenium: how to find element by text

Time:06-30

I'm trying to search element by text with selenium but didn't find anything that work. The element is displayed below:

<div ><span ><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.

I've tried multiples possibilities using XPath which didn't work.

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

Can someone help me ?

CodePudding user response:

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

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