打破外部循环错误

时间:2016-02-06 23:04:10

标签: python python-2.7

我是python的新手,我收到一条错误说明

  

打破外部循环

我知道休息只能在lopp中使用,但实际上我不知道何时确定循环结束。

如何通过将休息放在正确的位置来解决此错误(如果这是造成问题的原因)?

代码:

# 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

1 个答案:

答案 0 :(得分:0)

python中的

break在循环中使用。应该很容易找到循环,因为python代码需要正确缩进。代码中的break不在循环中,而是在try块中。类似的是继续。

我不确定逻辑,但可以通过在while循环

中添加/缩进以下try块来解决此问题
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  #this break is in the try block
except NoSuchElementException:
    continue