from selenium import webdriver from selenium.webdriver.common.by import By from common import MailProvider, MailMessage import json from time import sleep class ProtonWebmail(MailProvider): def transform_message_header(self, header): return header.replace('\n', ' - ').replace('Unread email - ', '') def get(self): self.messages_failed = 0 xpaths = { "username_box": "/html/body/div[1]/div[4]/div[1]/main/div[1]/div[2]/form/div[2]/div[1]/div/div/input", "password_box": "/html/body/div[1]/div[4]/div[1]/main/div[1]/div[2]/form/div[3]/div[1]/div/div[1]/input", "sign_in": "/html/body/div[1]/div[4]/div[1]/main/div[1]/div[2]/form/button", "messages": "/html/body/div[1]/div[3]/div/div[2]/div/div[2]/div/div/div/main/div/div/div/div/div/div[2]/div[2]", "messagebody": "/html/body/div[1]/div[3]/div/div[2]/div/div[2]/div/div/div/main/div/div/section/div/div[3]/div/div/article/div[2]", "backbutton": "//*[text()[contains(., 'Back')]]", } self.get_seen_messages() self._get_webdriver() self.webdriver.get("https://account.proton.me/login") self._wait_for_elem(xpaths["username_box"]) sleep(0.5) self._type_in_elem(xpaths["username_box"], self.username) self._click_elem(xpaths["password_box"]) self._type_in_elem(xpaths["password_box"], self.password) self._click_elem(xpaths["sign_in"]) self._wait_for_elem(xpaths["messages"]) count = 0 for i in self._get_elem_children(self._to_elem(xpaths["messages"])): if not self.is_seen(i.text): count += 1 if count == 3: break text = i.text # we can interact with it, just selenium doesn't like it try: self._click_elem(i) except ElementNotInteractableException: pass self._wait_for_elem(xpaths["messagebody"]) sleep(5) self.webdriver.switch_to.frame(self._to_elem(xpaths["messagebody"]).find_elements(By.XPATH, "//iframe")[0]) message = MailMessage( self.transform_message_header(text), "", self.webdriver.page_source, ) self.add_to_seen(message) self.webdriver.switch_to.default_content() sleep(2) self._click_elem(xpaths["backbutton"]) sleep(2) self.write_seen_messages() self.webdriver.quit()