Selenium: how to find element by text

Issue

I’m trying to search element by text with selenium but didn’t find anything that work. 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.

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 ?

Solution

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

Answered By – F.Hoque

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published