0
<div id="MainCopy_ctl13_presentJob_EmailAddressPanel">
    <a id="MainCopy_ctl13_presentJob_EmailAddress" href="mailto:dburse@bjcta.org">xyzmmm@tccp.org</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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
vamsi
  • 3
  • 4
  • browser.find_elements_by_xpath why are using elements use browser.find_element_by_xpath instead – PDHide Feb 06 '21 at 13:21

4 Answers4

1

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:dburse@bjcta.org">xyzmmm@tccp.org</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)
Frederick
  • 450
  • 4
  • 22
0

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()
0

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)
PDHide
  • 18,113
  • 2
  • 31
  • 46
  • AttributeError: 'list' object has no attribute 'get_attribute' – vamsi Feb 06 '21 at 13:19
  • @vamsi just change it to element updated the code , why were using elements – PDHide Feb 06 '21 at 13:20
  • email = browser.find_element_by_xpath('//div[@id="MainCopy_ctl13_presentJob_EmailAddress"]//a').get_attribute('textContent') – vamsi Feb 06 '21 at 13:28
  • File "C:\Users\yyy\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath return self.find_element(by=By.XPATH – vamsi Feb 06 '21 at 13:31
  • is it element not found ? – PDHide Feb 06 '21 at 13:32
  • @vamsi your id is wrong updated it MainCopy_ctl13_presentJob_EmailAddressPanel try now – PDHide Feb 06 '21 at 13:33
  • Traceback (most recent call last): File "C:\jk\APTA\main.py", line 140, in main() File "C:\jk\APTA\main.py", line 61, in main email = browser.find_element_by_xpath('//div[@id="MainCopy_ctl13_presentJob_EmailAddress"]//a').get_attribute('textContent') File "C:\Users\yyy\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath) – vamsi Feb 06 '21 at 13:34
  • File "C:\Users\yyy\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element return self.execute(Command.FIND_ELEMENT, { File "C:\Users\yyy\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) – vamsi Feb 06 '21 at 13:34
  • File "C:\Users\yyy\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@id="MainCopy_ctl13_presentJob_EmailAddress"]//a"} – vamsi Feb 06 '21 at 13:35
  • you should use MainCopy_ctl13_presentJob_EmailAddressPanel i have updated the code please try that – PDHide Feb 06 '21 at 13:35
  • Please accept the answer by clicking the tick sign near to my answer – PDHide Feb 08 '21 at 08:25
  • Sure, can u help me with another one – vamsi Feb 08 '21 at 09:01
  • browser.find_element_by_xpath('//div[@id="MainCopy_ctl13_presentJob_EmailAddressPanel"]//a').get_attribute("textContent") how can we print 'no data' if email address is not present in that xpath – vamsi Feb 08 '21 at 09:02
  • a =browser.find_element_by_xpath('//div[@id="MainCopy_ctl13_presentJob_EmailAddressPanel"]//a').get_attribute("textContent") ; if (a==""){a="no data"} add an if condition – PDHide Feb 08 '21 at 10:01
  • i tried getting error selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@id="MainCopy_ctl13_presentJob_EmailAddressPanel"]//a"} – vamsi Feb 08 '21 at 10:06
  • i am using looping, for first one i dont have email address, for second one i have email address – vamsi Feb 08 '21 at 10:08
  • inside loop add a if condition – PDHide Feb 08 '21 at 10:33
  • email = browser.find_element_by_xpath('//div[@id="MainCopy_ctl13_presentJob_EmailAddressPanel"]//a').get_attribute("textContent") if(email == ""): email = "No Email" – vamsi Feb 08 '21 at 10:34
0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352