break outside loop error

Ich bin neu in Python und erhalte die Fehlermeldung

break outside loop

Ich weiß, dass eine Pause nur innerhalb eines Lopps verwendet werden kann, aber ich habe eigentlich keine Ahnung, wann sie bestimmt, wann eine Schleife endet.

Wie kann ich diesen Fehler beheben, indem ich die Unterbrechung an der richtigen Stelle platziere (wenn dies der Grund für das Problem ist)?

Code

# see if we have an available date in this month
try:
    next_available_date = current_date.find_element_by_xpath("following::td[@data-handler='selectDay' and ancestor::div/@id='departureDateContainer']")
    print("Found an available date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year))
    next_available_date.click()
except NoSuchElementException:
# looping over until the next available date found
        while True:
# click next, if not found, select the next year
            try:
                calendar.find_element_by_class_name("ui-datepicker-next").click()
            except NoSuchElementException:
# select next year
                year = Select(calendar.find_element_by_class_name("ui-datepicker-year"))
                year.select_by_visible_text(str(int(year.first_selected_option.text) + 1))

# reporting current processed month and year
                month = Select(calendar.find_element_by_class_name("ui-datepicker-month")).first_selected_option.text
                year = Select(calendar.find_element_by_class_name("ui-datepicker-year")).first_selected_option.text
                print("Processing {month} {year}".format(month=month, year=year))

try:
    next_available_date = calendar.find_element_by_xpath(".//td[@data-handler='selectDay']")
    print("Found an available date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year))
    next_available_date.click()
    break
except NoSuchElementException:
    continue

Antworten auf die Frage(2)

Ihre Antwort auf die Frage