0

I am using python selenium and trying to get the email listed below:

 <a class="contactAction-214" aria-label="Email example@example.com" tabindex="0" draggable="false" href="mailto:example@example.com"><i data-icon-name="Mail" aria-hidden="true" class="icon-224"></i><span class="text-219"><span>example@example.com</span></span></a>

I am trying to get the email: example@example.com above; I have been stuck on this for about an hour now and I think the easiest way to do this would be to get the href or aria-label, any help appriceated!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Does this answer your question? [How to get attribute of element from Selenium?](https://stackoverflow.com/questions/30324760/how-to-get-attribute-of-element-from-selenium) – JaSON Nov 29 '21 at 07:02

2 Answers2

0

You could try it like this. You first find the Xpath to the particular part of the HTML you are looking for and then you ask for the aria-label attribute

email = driver.find_element_by_xpath("//a[@class='contactAction-214']").get_attribute("aria-label")
0

To print the text example@example.com you can use either of the following Locator Strategies:

  • Using css_selector and get_attribute("innerHTML"):

    print(driver.find_element(By.CSS_SELECTOR, "a[class^='contactAction'][aria-label^='Email'][href] span[class] > span").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element(By.XPATH, "//a[starts-with(@class, 'contactAction') and starts-with(@aria-label, 'Email')][@href]//span[@class]/span").text)
    

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[class^='contactAction'][aria-label^='Email'][href] span[class] > span"))).text)
    
  • Using XPATH and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[starts-with(@class, 'contactAction') and starts-with(@aria-label, 'Email')][@href]//span[@class]/span"))).get_attribute("innerHTML"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


References

Link to useful documentation:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352