-1

I am use python and selenium to go to a website that generates a random email. I am trying to store that email in a variable and return it.

def getemail(self):

    driver = self.driver
    driver.get("https://temp-mail.org/en/")
    time.sleep(2)
    ne = driver.find_element_by_xpath("//*[@id='mail']").text
    return ne
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
delinoss
  • 3
  • 1
  • Welcome to Stack Overflow! This is the kind of Selenium issue that comes up a lot, but isn't really a good SO question; it's likely that your locator is not correctly written. Please follow [this guide](https://openwritings.net/pg/python/python-selenium-debugging) on debugging issues where you can't find an element in Selenium. – Niayesh Isky Aug 25 '19 at 23:18
  • Are you sure that driver.get("https://temp-mail.org/en/") is working? Try to print "driver" variable first. So, you can know that problem is here or in next lines :) – Scott Aug 25 '19 at 23:49
  • 1
    Possible duplicate of [Selenium Webdriver Python How to get text from the input tag](https://stackoverflow.com/questions/32630986/selenium-webdriver-python-how-to-get-text-from-the-input-tag) – JeffC Aug 25 '19 at 23:54

3 Answers3

1

It is <input> and email is in its attribute value="", not as text between opening and closing tag.

ne = driver.find_element_by_xpath("//*[@id='mail']").get_attribute("value")

EDIT: as suggested @JeffC in comment it could use find_element_by_id

ne = driver.find_element_by_id("mail").get_attribute("value")
furas
  • 134,197
  • 12
  • 106
  • 148
  • 1
    If you are going to search for an element by nothing but an ID, please please please use `*_by_id()`. There's no reason not to. I know OP posted that but we should suggest best practices when we can improve their code, even if it's not what was specifically asked. – JeffC Aug 25 '19 at 23:52
  • 1
    @JeffC you are right. Frankly, I didn't even look at xpath. I only checked if there is no `_elements_` with `"s"` at the end :) – furas Aug 26 '19 at 00:00
0

@furas answer was pretty close. However to extract the random email address you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#mail"))).get_attribute("value"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='mail']"))).get_attribute("value"))
    
  • 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
0

Just get this by get_attribute.

Change the second last line of your code to this:

ne = driver.find_element_by_id("mail").get_attribute("value")