0
emailcontent = []
def getEmails(self):
    self.browser.get("https://outlook.live.com/mail/0/inbox")
    time.sleep(3)
    content = self.browser.find_elements_by_css_selector("._2ZDUqsleGa-jar5wAYvVzV") 
    for c in content:
        c.click() # opens the email
    content = self.browser.find_element_by_dir("Itr").text 
    # div dir="Itr" only common tag for email contents
    emailcontent.append(content)

getEmails()

txt = ""
for a in emailcontent:
    txt += "{:<30} \n".format(a)
    with open("new.txt", "a") as output:
        output.write(txt)

os.startfile('new.txt')   

I have a code as above, previous parts of codes basically enters my hotmail/outlook account and no any problem with them. I want this part to open every single email and save content to txt file but it doesnt happen. Only common tag for email contents are div dir="Itr" and I dont know how to find_element_... them one by one. Probably I need to use loops but I couldn't make it. Anyone wants to help?

eyup.tatar
  • 75
  • 8

1 Answers1

1

Here

content = self.browser.find_elements_by_css_selector("._2ZDUqsleGa-jar5wAYvVzV") 

You have to improve the locator. Otherwise the first matching element is not what you want.
Take this instead:

content = self.browser.find_elements_by_css_selector("div._2ZDUqsleGa-jar5wAYvVzV") 

after expanding each email you can retrieve the content with div.rps_b62 css_selector.
So you code can be:

content = self.browser.find_elements_by_css_selector("div._2ZDUqsleGa-jar5wAYvVzV") 
for c in content:
    c.click()
    time.sleep(1)
    content = self.browser.find_element_by_css_selector("div.rps_b62").text 
    emailcontent.append(content)

taking the content and appending it to list should be done inside the for loop.
It also should be a delay there to let the content opened correctly.
As the above.

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • div.rps_b62 is not common css selector, if you check the other emails you see they are all different, but thank you for the loop – eyup.tatar Aug 19 '21 at 17:36
  • yep unfortunately, div dir"ltr" is only common tag for emails – eyup.tatar Aug 19 '21 at 17:45
  • OK, I will be back later and will try to help with that – Prophet Aug 19 '21 at 17:55
  • _2Qk4AbDuWwkuLB005ds2jm QMubUjbS-BOly_BTHEZj7 allowTextSelection I tried to use this div class but it doesnt work either. Error code here: NoSuchElementException: Message: no such element: Unable to locate element . I added dot to blanks but same error again – eyup.tatar Aug 19 '21 at 18:02
  • I rechecked that. dir='ltr' or dir='rtl' is not a locator you can use here. While `div.rps_b62` css selector is matching content of all the mails I saw. There are many emails without content text inside, in this case there will be no such element, but all the emails containing text content will have this element. – Prophet Aug 19 '21 at 21:01
  • rps_cba for me, and as I understand there are different class names for different kind of emails, some have h1 some have td you know, thank you for your time – eyup.tatar Aug 20 '21 at 18:58
  • btw class name keeps changing all the time :) except for the mainpage classes – eyup.tatar Aug 20 '21 at 19:20