I'm a beginner developer in Python and I need some tips to improve my code, in performance (speed) and hardware usage. Is it possible to improve?
And I have a question: is it possible to keep the browser (chrome/chromedriver) open after execution?
The objective: to automate scheduling at the Italian Consulate, through the website prenot@mi. Booking on the Prenot@mi system is "simple", but it's very busy, so I have to be quicker than others to get a reservation. Within seconds, a booking is "lost" as another scheduler was able to book faster. Dates are available on Mondays and Wednesdays at 11:00 am.
My repository: https://github.com/skynorreply/prenot-mi_script
import datetime
import time
import os
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.alert import Alert
def esta_na_hora(hora, minuto, segundos, data_atual):
if data_atual.hour == hora and data_atual.minute == minuto and data_atual.second == segundos:
return True
return False
def processa_dias_da_semana(dias_da_semana):
dias_da_semana_int = []
for dia in dias_da_semana:
if dia == "seg":
dias_da_semana_int.append(0)
if dia == "ter":
dias_da_semana_int.append(1)
if dia == "qua":
dias_da_semana_int.append(2)
if dia == "qui":
dias_da_semana_int.append(3)
if dia == "sex":
dias_da_semana_int.append(4)
if dia == "sab":
dias_da_semana_int.append(5)
if dia == "dom":
dias_da_semana_int.append(6)
return dias_da_semana_int
def esta_no_dia_da_semana(dias_da_semana, data_atual):
if data_atual.weekday() in dias_da_semana:
return True
return False
hora_string = input("Que horas quer agendar? (hh:mm:sg): ")
dia_da_semana_string = input(
"Quais dias da semana? (seg ter qua qui sex sab dom): ")
hora = int(hora_string.split(':')[0])
minuto = int(hora_string.split(':')[1])
segundos = int(hora_string.split(':')[2])
dias_da_semana = dia_da_semana_string.split(' ')
dias_da_semana_int = processa_dias_da_semana(dias_da_semana)
ativo = True
while ativo:
agora = datetime.datetime.now()
print(agora)
if esta_na_hora(hora, minuto, segundos, agora) and esta_no_dia_da_semana(dias_da_semana_int, agora):
ativo = False
chrome_options = webdriver.ChromeOptions() #here begins to configure chrome not to load the images
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
navegador = webdriver.Chrome(chrome_options=chrome_options) #here finish configuring chrome to not load the images
navegador.get("https://prenotami.esteri.it") #access the site
time.sleep(3) #to wait the loading, only by safety
navegador.find_element(
By.ID, "login-email").send_keys("[email protected]") #REMEMBER TO PUT THE EMAIL
navegador.find_element(
By.ID, "login-password").send_keys("password") #REMEMBER TO PUT THE PASSWORD
navegador.find_element(
By.XPATH, '//*[@id="login-form"]/button').click() #will click on the button to login
time.sleep(3) #this time is important!!! You run the risk of failing.
navegador.find_element(By.ID, "advanced").click() #click on the book section
time.sleep(2)
navegador.find_element(
By.XPATH, '//*[@id="dataTableServices"]/tbody/tr[2]/td[4]/a/button').click() #here the system will click on the option Reserve Citizenship
time.sleep(2)
i = 1
while navegador.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button') == navegador.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button'):
print("Tentativa Nº - " + str(i))
navegador.find_element(
By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button').click()
delay = 10 #seconds
try:
elemento = WebDriverWait(navegador, delay).until(EC.presence_of_element_located(
(By.XPATH, '//*[@id="dataTableServices"]/tbody/tr[2]/td[4]/a/button')))
elemento.click()
except TimeoutException:
print("elemento nao encontrado")
i = i + 1
time.sleep(1)
navegador.find_element(By.ID, 'File_0').send_keys(os.path.abspath("/Users/compCOPEL.pdf")) #proof of address
navegador.find_element(By.ID, 'PrivacyCheck').click() #accept the privacy term
navegador.find_element(By.ID, 'btnAvanti').click() #forward button to calendar view
alert = Alert(navegador) #here is to identify the window that opens to confirm the appointment
alert.accept() #here will accept confirmation window
Thank you in advance!