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

locate an a element and click on it in selenium pyhton

I have this page and I want to click on the a element that sends an email(the one that is highlighted in the screenshot below enter image description here

and I have tried to find this element using By.XPATH
email = driver.find_element(By.XPATH, "//a[contains(@class, 'email-old-32')]").click()

and By.CLASS_NAME

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

email = driver.find_element(By.CLASS_NAME, 'email-old-32').click()

and in both situations i’m getting an error no such element, does anyone know what am I doing wrong?

>Solution :

There are several possible thing that may cause this:

  1. You need to wait for element to be loaded and to become clickable. WebDriverWait expected_conditions can be used for that.
    If this is your case instead of email = driver.find_element(By.XPATH, "//a[contains(@class, 'email-old-32')]").click() try this:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@class, 'email-old-32')]"))).click()
  1. The element class attribute value can be dynamic. Make sure it is not changing. In case it does – you need to find another, stable locator for that element.
  2. The element can be inside iframe. In this case you will need to switch into the iframe.
  3. Possibly you opened a new tab where the goal element is presented but forget to switch to a new tab.
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