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
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:
- 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 ofemail = 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()
- 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.
- The element can be inside
iframe
. In this case you will need to switch into theiframe
. - Possibly you opened a new tab where the goal element is presented but forget to switch to a new tab.