I’m having problems with the following implementation, my process_state_inclusion function should follow the following flow: step 1, step 1.2, step 1.3, step 1.4 if the state_inclusion button is not present on the page, go to step 1.4.1 and then go to step 1.5 and follows the normal flow, if the btn_action_4 button is present on the page, go to step 1.5 until step 1.9, if the state_inclusion button is not present on the page, go to step 1 9.1 and if step 1.4.1 and 1.9.1 for the process_state_inclusion value that is being processed by the loop, the application stops execution for this current state_inclusion value and moves to the next state_inclusion value of the loop from step 1 and the value that gave the error is stored in an array of failures, if in step 1.9 the btn_action_4 button is present on the page, go to step 1.10 and then continue until the end of the cycle for the first state_inclusion value. The flow is repeated to give the values. In this case, you would only interrupt the application and move to the next value in the loop if you execute steps 1.4.1 and 1.9.1, but if you execute only one of the two, the flow should continue normally. But whenever I run it, the flow crashes the application. And it doesn’t continue or go to the next value in the loop.
def process_state_inclusion(self, period, state_inclusion):
try:
# Step 1
self.wait.until(EC.element_to_be_clickable((By.ID, “first_field”)))
self.driver.find_element(By.ID, “first_field”).clear()
self.driver.find_element(By.ID, “first_field”).send_keys(period)
logger.info(f”Filled ‘first_field’ with ‘{period}'”)
time.sleep(1)
# Step 1.2
self.wait.until(EC.element_to_be_clickable((By.ID, "state_id_number")))
self.driver.find_element(By.ID, "state_id_number").clear()
self.driver.find_element(By.ID, "state_id_number").send_keys(state_inclusion)
logger.info(f"Filled 'state_id_number' with '{state_inclusion}'")
time.sleep(1)
# Step 1.3
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_generate_non_calculated_report"))).click()
logger.info("Clicked on the 'btn_generate_non_calculated_report' button")
time.sleep(3) # Wait for download
# Step 1.4 and 1.4.1
try:
self.wait.until(EC.element_to_be_clickable((By.NAME, "btn_action_4")), 5).click()
logger.info("Clicked on the 'btn_action_4' button again")
time.sleep(1)
except:
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_proceed"))).click()
logger.info("No records found for the provided parameters!")
logger.info("Clicked on the 'btn_proceed' button to continue")
time.sleep(1)
return
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_proceed"))).click()
logger.info("Clicked on the 'btn_proceed' button")
time.sleep(1)
# Repeat the flow for Calculated
self.wait.until(EC.element_to_be_clickable((By.ID, "first_field")))
self.driver.find_element(By.ID, "first_field").clear()
self.driver.find_element(By.ID, "first_field").send_keys(period)
logger.info(f"Filled 'first_field' with '{period}'")
time.sleep(1)
self.wait.until(EC.element_to_be_clickable((By.ID, "state_id_number")))
self.driver.find_element(By.ID, "state_id_number").clear()
self.driver.find_element(By.ID, "state_id_number").send_keys(state_inclusion)
logger.info(f"Filled 'state_id_number' with '{state_inclusion}'")
time.sleep(1)
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_generate_calculated_report"))).click()
logger.info("Clicked on the 'btn_generate_calculated_report' button")
time.sleep(3) # Wait for download
try:
self.wait.until(EC.element_to_be_clickable((By.NAME, "btn_action_4")), 5).click()
logger.info("Clicked on the 'btn_action_4' button again")
time.sleep(1)
except:
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_proceed"))).click()
logger.info("No records found for the provided parameters!")
logger.info("Clicked on the 'btn_proceed' button to continue")
time.sleep(1)
return
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_proceed"))).click()
logger.info("Clicked on the 'btn_proceed' button")
time.sleep(1)
# Repeat the flow for Debits
self.wait.until(EC.element_to_be_clickable((By.ID, "first_field")))
self.driver.find_element(By.ID, "first_field").clear()
self.driver.find_element(By.ID, "first_field").send_keys(period)
logger.info(f"Filled 'first_field' with '{period}'")
time.sleep(1)
self.wait.until(EC.element_to_be_clickable((By.ID, "state_id_number")))
self.driver.find_element(By.ID, "state_id_number").clear()
self.driver.find_element(By.ID, "state_id_number").send_keys(state_inclusion)
logger.info(f"Filled 'state_id_number' with '{state_inclusion}'")
time.sleep(1)
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_generate_debit_report"))).click()
logger.info("Clicked on the 'btn_generate_debit_report' button")
time.sleep(3) # Wait for download
try:
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_generate_statement")), 5).click()
logger.info("Clicked on the 'btn_generate_statement' button")
time.sleep(3) # Wait for download
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_generate_statement"))).click()
logger.info("Clicked on the 'btn_generate_statement' button again")
time.sleep(3) # Wait for download
except:
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_proceed"))).click()
logger.info("No records found for the provided parameters!")
logger.info("Clicked on the 'btn_proceed' button to continue")
time.sleep(1)
return
self.wait.until(EC.element_to_be_clickable((By.NAME, "btn_action_4"))).click()
logger.info("Clicked on the 'btn_action_4' button again")
time.sleep(1)
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_proceed"))).click()
logger.info("Clicked on the 'btn_proceed' button")
time.sleep(1)
except Exception as e:
logger.error(f"Error processing state registration {state_inclusion}: {e}")
def download_notes(self, state_inclusions, period):
try:
self.wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Contributor Statement - CONTESTATION"))).click()
logger.info("Clicked on the 'Contributor Statement - CONTESTATION' link")
time.sleep(1)
for state_inclusion in state_inclusions:
self.process_state_inclusion(period, state_inclusion)
self.wait.until(EC.element_to_be_clickable((By.ID, "btn_cancel"))).click()
logger.info("Clicked on the 'btn_cancel' button")
time.sleep(1)
self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span > .fa-user"))).click()
logger.info("Clicked on the user icon")
time.sleep(1)
self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#link_logout > span"))).click()
logger.info("Clicked on the link to log out")
time.sleep(1)
alert = self.driver.switch_to.alert
assert alert.text == "Ao encerrar a sessão solicitaremos o fechamenton"
"do browser para evitar o uso indevido do certificado digital.nn"
"Confirma o encerramento da sessão?"
alert.accept()
logger.info("Accepted the confirmation alert")
except Exception as e:
logger.error(f"Error downloading the notes: {e}")
raise
Loop can’t get past errors
Charles Henrique is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.