1

I would like to get emails from https://spamwc.de/ using Selenium (Python).

In html DOM it looks like: document.getElementById("mails").textContent (it works, when no emails in mailbox it gives "...no mails...", and it's fine)

I would like to get the same result using Selenium.

My code:

        try:
            mails = self.driver.find_element_by_id("mails").getAttribute("outerHTML")
            print("mails:", mails)
        except:
            print("mails: no outerHTML")

        try:
            mails = self.driver.find_element_by_id("mails").getAttribute("innerHTML")
            print("mails:", mails)
        except:
            print("mails: no innerHTML")

        try:
            mails = self.driver.find_element_by_id("mails").getAttribute("textContent")
            print("mails:", mails)
        except:
            print("mails: no textContent")

Result (exceptions: 'WebElement' object has no attribute 'getAttribute'):

mails: no outerHTML
mails: no innerHTML
mails: no textContent

Of course the element with id="mails" exists.

Example when mailbox (e.g. test@akkecuwa.ga) is empty: document.getElementById('mails').textContent

Result: There are no mails for you, sorry.

I am open to any suggestions.

Novik
  • 88
  • 1
  • 6
  • I cant see document.getElementById("mails").textContent in the above link https://spamwc.de/. Can you please send some other reference? – Shweta Valunj Aug 12 '19 at 12:30
  • Sorry. After you go to spamwc.de you must fill name of email and press "Send". Then you will be redicted to mailbox with mails. – Novik Aug 12 '19 at 12:57
  • You can get by xpath `driver.find_element_by_xpath("//*[@id="mails"]/span").text` – Shweta Valunj Aug 12 '19 at 13:19
  • I am not familiar with xpath. Exception: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='mails'/span]"} – Novik Aug 12 '19 at 13:29
  • 1
    try logging entire html before the `driver.find` line like this `browser.get(yourUrl) html_source = browser.page_source` And see if it has the element you are looking for. – Shweta Valunj Aug 12 '19 at 13:36
  • Possible duplicate of [How to get text with selenium web driver in python](https://stackoverflow.com/questions/20996392/how-to-get-text-with-selenium-web-driver-in-python) – JeffC Aug 12 '19 at 14:02
  • Shweta Valunj, your code works. I added 3s of sleep after press "Send" button. I think the problem was the content wan't loaded yet. Thank you. – Novik Aug 12 '19 at 14:06

2 Answers2

1

The method you want to call is get_attribute(), not getAttribute(). It should work fine after that.

    try:
        mails = self.driver.find_element_by_id("mails").get_attribute("outerHTML")
        print("mails:", mails)
    except:
        print("mails: no outerHTML")

    try:
        mails = self.driver.find_element_by_id("mails").get_attribute("innerHTML")
        print("mails:", mails)
    except:
        print("mails: no innerHTML")

    try:
        mails = self.driver.find_element_by_id("mails").get_attribute("textContent")
        print("mails:", mails)
    except:
        print("mails: no textContent")
0

I assume that You're probably willing to get the Text out of these emails.

mails = self.driver.find_element_by_id("mails").get_attribute("outerHTML")
content = mails.text //just search for the correct method to get this text

There are probably more than one mails that You'd like to check, so instead of this find_element_by_id try find_elements_by_id (which will find all elements that fits your selector) then You can loop through list of these elements, and get their content for each of this elements included in the list.

ArturS
  • 723
  • 1
  • 6
  • 19
  • 1
    is this relevant to python? – WiseDev Aug 12 '19 at 12:37
  • Good catch, I've removed the unnecessary "var" from this variable and the semicolons – ArturS Aug 12 '19 at 12:45
  • Thank you for your prompt reply. Yes, I want to get the text of these emails. In HTML (document.getElementById("mails").textContent) all emails are presented together. No loop is required. Mails are not presented in HTML page source. Your code caused exception 'WebElement' object has no attribute 'getAttribute'. – Novik Aug 12 '19 at 13:10
  • That's because it should be get_attribute instead of this getAttribute, I've updated the answer – ArturS Aug 13 '19 at 11:10