Reputation: 75
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?
Upvotes: 0
Views: 502
Reputation: 33361
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.
Upvotes: 1