Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
Save screenshots and html when selenium error
Browse files Browse the repository at this point in the history
  • Loading branch information
sinun98 committed May 26, 2021
1 parent 46ef022 commit 89d3d47
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions tools/its.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@

try:
import beepy

ENABLE_BEEPY = True
except ImportError:
ENABLE_BEEPY = False


class ImpfterminService():
def __init__(self, code: str, plz_impfzentren: list, kontakt: dict,PATH:str):
def __init__(self, code: str, plz_impfzentren: list, kontakt: dict, PATH: str):
self.code = str(code).upper()
self.splitted_code = self.code.split("-")

Expand Down Expand Up @@ -151,7 +153,6 @@ def impfstoffe_laden(self):
self.log.error("Keine Impfstoffe im ausgewählten Impfzentrum verfügbar")
return False


def get_chromedriver_path(self):
"""
:return: String mit Pfad zur chromedriver-Programmdatei
Expand All @@ -176,7 +177,6 @@ def get_chromedriver_path(self):
else:
raise ValueError(f"Nicht unterstütztes Betriebssystem {self.operating_system}")


def get_chromedriver(self, headless):
chrome_options = Options()

Expand All @@ -194,7 +194,6 @@ def get_chromedriver(self, headless):

return Chrome(self.get_chromedriver_path(), options=chrome_options)


def driver_enter_code(self, driver, plz_impfzentrum):
"""
TODO xpath code auslagern
Expand Down Expand Up @@ -224,16 +223,16 @@ def driver_enter_code(self, driver, plz_impfzentrum):

# Klick auf "Vermittlungscode bereits vorhanden"
button_xpath = "/html/body/app-root/div/app-page-its-login/div/div/div[2]/app-its-login-user/" \
"div/div/app-corona-vaccination/div[2]/div/div/label[1]/span"
"div/div/app-corona-vaccination/div[2]/div/div/label[1]/span"
button = WebDriverWait(driver, 1).until(
EC.element_to_be_clickable((By.XPATH, button_xpath)))
action = ActionChains(driver)
action.move_to_element(button).click().perform()

# Auswahl des ersten Code-Input-Feldes
input_xpath = "/html/body/app-root/div/app-page-its-login/div/div/div[2]/app-its-login-user/" \
"div/div/app-corona-vaccination/div[3]/div/div/div/div[1]/app-corona-vaccination-yes/" \
"form[1]/div[1]/label/app-ets-input-code/div/div[1]/label/input"
"div/div/app-corona-vaccination/div[3]/div/div/div/div[1]/app-corona-vaccination-yes/" \
"form[1]/div[1]/label/app-ets-input-code/div/div[1]/label/input"
input_field = WebDriverWait(driver, 1).until(
EC.element_to_be_clickable((By.XPATH, input_xpath)))
action = ActionChains(driver)
Expand All @@ -245,8 +244,8 @@ def driver_enter_code(self, driver, plz_impfzentrum):

# Klick auf "Termin suchen"
button_xpath = "/html/body/app-root/div/app-page-its-login/div/div/div[2]/app-its-login-user/" \
"div/div/app-corona-vaccination/div[3]/div/div/div/div[1]/app-corona-vaccination-yes/" \
"form[1]/div[2]/button"
"div/div/app-corona-vaccination/div[3]/div/div/div/div[1]/app-corona-vaccination-yes/" \
"form[1]/div[2]/button"
button = WebDriverWait(driver, 1).until(
EC.element_to_be_clickable((By.XPATH, button_xpath)))
action = ActionChains(driver)
Expand All @@ -255,7 +254,6 @@ def driver_enter_code(self, driver, plz_impfzentrum):
# Maus-Bewegung hinzufügen (nicht sichtbar)
action.move_by_offset(10, 20).perform()


def driver_renew_cookies(self, driver, plz_impfzentrum):
self.driver_enter_code(driver, plz_impfzentrum)

Expand All @@ -273,9 +271,8 @@ def driver_renew_cookies(self, driver, plz_impfzentrum):
except:
return False



def driver_book_appointment(self, driver, plz_impfzentrum):
PATH = os.path.dirname(os.path.realpath(__file__))
url = f"{self.domain}impftermine/service?plz={plz_impfzentrum}"

self.driver_enter_code(driver, plz_impfzentrum)
Expand All @@ -287,13 +284,20 @@ def driver_book_appointment(self, driver, plz_impfzentrum):
EC.element_to_be_clickable((By.XPATH, button_xpath)))
action = ActionChains(driver)
action.move_to_element(button).click().perform()
time.sleep(.5)
except:
self.log.error("Termine können nicht gesucht werden")
try:
filepath = os.path.join(PATH, "log/errorterminsuche/")
timestamp = str(datetime.datetime.now()).replace(":", "").split(".")[0]
driver.get_screenshot_as_file(filepath + "errorterminsuche" + timestamp + ".jpg")
except:
self.log.error("Screenshot konnte nicht gespeichert werden")
pass
pass

# Termin auswählen
try:
time.sleep(3)
button_xpath = '//*[@id="itsSearchAppointmentsModal"]/div/div/div[2]/div/div/form/div[1]/div[2]/label/div[2]/div'
button = WebDriverWait(driver, 1).until(
EC.element_to_be_clickable((By.XPATH, button_xpath)))
Expand All @@ -302,9 +306,18 @@ def driver_book_appointment(self, driver, plz_impfzentrum):
time.sleep(.5)
except:
self.log.error("Termine können nicht ausgewählt werden")
try:
PATH = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(PATH, "log/errorterminauswahl/")
timestamp = str(datetime.datetime.now()).replace(":", "").split(".")[0]
with open(filepath + "errorterminauswahl" + timestamp + ".html", 'w', encoding='utf-8') as file:
file.write(str(driver.page_source))
driver.get_screenshot_as_file(filepath + "errorterminauswahl" + timestamp + ".jpg")
except:
self.log.error("HTML und Screenshot konnten nicht gespeichert werden")
pass
pass


# Klick Button "AUSWÄHLEN"
try:
button_xpath = '//*[@id="itsSearchAppointmentsModal"]/div/div/div[2]/div/div/form/div[2]/button[1]'
Expand Down Expand Up @@ -384,6 +397,13 @@ def driver_book_appointment(self, driver, plz_impfzentrum):
input_field.send_keys(self.kontakt['notificationReceiver'])
except:
self.log.error("Kontaktdaten können nicht eingegeben werden")
try:
filepath = os.path.join(PATH, "log/errordateneingeben/")
timestamp = str(datetime.datetime.now()).replace(":", "").split(".")[0]
driver.get_screenshot_as_file(filepath + "errordateneingeben" + timestamp + ".jpg")
except:
self.log.error("Screenshot konnte nicht gespeichert werden")
pass
pass

# Klick Button "ÜBERNEHMEN"
Expand Down Expand Up @@ -412,15 +432,15 @@ def driver_book_appointment(self, driver, plz_impfzentrum):
if "Ihr Termin am" in str(driver.page_source):
msg = "Termin erfolgreich gebucht!"
self.log.success(msg)
desktop_notification(operating_system=self.operating_system,title="Terminbuchung:",message=msg)
desktop_notification(operating_system=self.operating_system, title="Terminbuchung:", message=msg)
return True
else:
self.log.error("Automatisierte Terminbuchung fehlgeschlagen. Termin manuell im Fenster oder im Browser buchen.")
self.log.error(
"Automatisierte Terminbuchung fehlgeschlagen. Termin manuell im Fenster oder im Browser buchen.")
print("Link für manuelle Buchung im Browser:", url)
time.sleep(10*60)
time.sleep(10 * 60)
return False


@retry_on_failure()
def renew_cookies(self):
"""
Expand All @@ -432,7 +452,6 @@ def renew_cookies(self):
with self.get_chromedriver(headless=True) as driver:
return self.driver_renew_cookies(driver, choice(self.plz_impfzentren))


@retry_on_failure()
def book_appointment(self):
"""
Expand Down Expand Up @@ -479,7 +498,6 @@ def login(self):
else:
return False


@retry_on_failure()
def termin_suchen(self, plz):
"""Es wird nach einen verfügbaren Termin in der gewünschten PLZ gesucht.
Expand Down Expand Up @@ -560,7 +578,7 @@ def termin_buchen(self):
if res.status_code == 201:
msg = "Termin erfolgreich gebucht!"
self.log.success(msg)
desktop_notification(operating_system=self.operating_system,title="Terminbuchung:",message=msg)
desktop_notification(operating_system=self.operating_system, title="Terminbuchung:", message=msg)
return True

elif res.status_code == 429:
Expand All @@ -579,7 +597,7 @@ def termin_buchen(self):
msg = f"Unbekannter Statuscode: {res.status_code}"

self.log.error(msg)
desktop_notification(operating_system=self.operating_system,title="Terminbuchung:", message=msg)
desktop_notification(operating_system=self.operating_system, title="Terminbuchung:", message=msg)
return False

@retry_on_failure()
Expand Down Expand Up @@ -650,7 +668,7 @@ def code_bestaetigen(self, token, sms_pin):
return False

@staticmethod
def terminsuche(code: str, plz_impfzentren: list, kontakt: dict,PATH:str, check_delay: int = 30):
def terminsuche(code: str, plz_impfzentren: list, kontakt: dict, PATH: str, check_delay: int = 30):
"""
Workflow für die Terminbuchung.
Expand All @@ -661,7 +679,7 @@ def terminsuche(code: str, plz_impfzentren: list, kontakt: dict,PATH:str, check_
:return:
"""

its = ImpfterminService(code, plz_impfzentren, kontakt,PATH)
its = ImpfterminService(code, plz_impfzentren, kontakt, PATH)
its.renew_cookies()

# login ist nicht zwingend erforderlich
Expand Down

0 comments on commit 89d3d47

Please sign in to comment.