vamsi
vamsi

Reputation: 3

How can I print email address using Selenium Python

<div id="MainCopy_ctl13_presentJob_EmailAddressPanel">
    <a id="MainCopy_ctl13_presentJob_EmailAddress" href="mailto:[email protected]">[email protected]</a>
</div>

I have tried using

email = browser.find_elements_by_xpath('//div[@id="MainCopy_ctl13_presentJob_EmailAddress"]//a').text
print(email)

But I'm not getting a result.

Upvotes: 0

Views: 190

Answers (4)

undetected Selenium
undetected Selenium

Reputation: 193108

The id attribute which you have used i.e. MainCopy_ctl13_presentJob_EmailAddress belongs to the <a> tag instead of the <div>

To print the email address you can use either of the following Locator Strategies:

  • Using css_selector and get_attribute():

    print(driver.find_element(By.CSS_SELECTOR, "a#MainCopy_ctl13_presentJob_EmailAddress").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element(By.XPATH, "//a[@id='MainCopy_ctl13_presentJob_EmailAddress']").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#MainCopy_ctl13_presentJob_EmailAddress"))).text)
    
  • Using XPATH and get_attribute():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@id='MainCopy_ctl13_presentJob_EmailAddress']"))).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
    

Upvotes: 0

PDHide
PDHide

Reputation: 19949

text print only visible text use textContent attribute for text not in display port:

email = browser.find_element_by_xpath('//div[@id="MainCopy_ctl13_presentJob_EmailAddressPanel"]//a').get_attribute("textContent")
print(email)

Upvotes: 0

Frederick
Frederick

Reputation: 470

The email inside the a tag is the href of the a tag so just do this:

Using Selenium:

from selenium import webdriver
    
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")


a_tag = driver.find_element_by_id('MainCopy_ctl13_presentJob_EmailAddress')
mail_link = a_tag.get_attribute("href")
mail_addrs = mail_link.split(':')[1]
print(mail_addrs)

Using Beautifulsoup:

from bs4 import BeautifulSoup
    
content=""" 
<div id="MainCopy_ctl13_presentJob_EmailAddressPanel">
    a id="MainCopy_ctl13_presentJob_EmailAddress" href="mailto:[email protected]">[email protected]</a>
</div>"""
soup = BeautifulSoup(content)
a_tag = soup.find(id='MainCopy_ctl13_presentJob_EmailAddress')
mail_link = a_tag.attrs['href']
mail_addrs = mail_link.split(':')[1]
print(mail_addrs)

Upvotes: 1

Armanda Arif Firdaus
Armanda Arif Firdaus

Reputation: 21

is the element already there? or perhaps code executed before the element is loaded by Selenium?

consider using wait :

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

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

Upvotes: 0

Related Questions