Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selenium: how to find element by text

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading