0
<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.

1
  • browser.find_elements_by_xpath why are using elements use browser.find_element_by_xpath instead Commented Feb 6, 2021 at 13:21

4 Answers 4

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:[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)
Sign up to request clarification or add additional context in comments.

4 Comments

in the place of content it is possible to store with xpath ot get element by id. i am details using xpath
So you want to find the email but using xpath instead of id?
i am using browser.find_element_by_id('MainCopy_ctl13_presentJob_EmailAddress')
we no need to use content variable
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)

18 Comments

AttributeError: 'list' object has no attribute 'get_attribute'
@vamsi just change it to element updated the code , why were using elements
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
is it element not found ?
|
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()

2 Comments

element present, just we need to print.
i have used chome web driver
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
    

1 Comment

how to return empty result Ex: no data if the xpath doesnot have data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.