I am using python selenium and trying to get the email listed below:
<a class="contactAction-214" aria-label="Email [email protected]" tabindex="0" draggable="false" href="mailto:[email protected]"><i data-icon-name="Mail" aria-hidden="true" class="icon-224"></i><span class="text-219"><span>[email protected]</span></span></a>
I am trying to get the email: [email protected] 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!
CodePudding user response:
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")
CodePudding user response:
To print the text [email protected]
you can use either of the following Locator Strategies:
Using
css_selector
andget_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
andget_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:
get_attribute()
methodGets the given attribute or property of the element.
text
attribute returnsThe text of the element.
- Difference between text and innerHTML using Selenium