Problems with loop error handling in Python

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)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> # 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
</code>
<code> # 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 </code>
        # 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

New contributor

Charles Henrique is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật