'selenium python create a if statement and break if the if statement is found

so am trying to implement a if statement when an element limit is found must click and break or else continue with the loop

my code below

all_buttons = driver.find_elements_by_tag_name("button")
    connect_buttons = [btn for btn in all_buttons if btn.text == "Connect" ]
    for btn in connect_buttons:
        driver.execute_script("arguments[0].click();", btn)
        time.sleep(2)
        send = driver.find_element_by_xpath("//button[@aria-label='Send now']")
        driver.execute_script("arguments[0].click();", send)
        time.sleep(2)
        limit = driver.find_element_by_xpath("//button[@aria-label='Got it']")
        if limit.is_displayed:
            driver.execute_script("arguments[0].click();",limit)
            print("limit reached")
            break


Solution 1:[1]

You can use try - except instead of if - else:

all_buttons = driver.find_elements_by_tag_name("button")
    connect_buttons = [btn for btn in all_buttons if btn.text == "Connect" ]
    for btn in connect_buttons:
        driver.execute_script("arguments[0].click();", btn)
        time.sleep(2)
        send = driver.find_element_by_xpath("//button[@aria-label='Send now']")
        driver.execute_script("arguments[0].click();", send)
        time.sleep(2)
        flag = True
        while flag:
            try:
                limit = driver.find_element_by_xpath("//button[@aria-label='Got it']")
            driver.execute_script("arguments[0].click();",limit)
                print("limit reached")
                flag = False
            except:
                flag = True

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1