Home > OS >  locate an a element and click on it in selenium pyhton
locate an a element and click on it in selenium pyhton

Time:10-24

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

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?

CodePudding user response:

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.
  • Related