Satish Ghanta
Satish Ghanta

Reputation: 11

How to break or exit from the loop when there are no more months to check

I'm trying to achieve a task, to fetch the text of Month and year and if those matches with the desired month and year then code should fetch the available days and click/select the desired date. If the desired month and year are not available then code should click the next arrow/button, also code should break/exit from the loop if there are no months to check.

Note: The calendar widget contains only 4 months like the current month and next 3 months Eg: Feb, mar, apr and may. Also, the next arrow/button is not visible to user on the last month. But, the respective xpath finds the next arrow/button on the last month too.

xpath of the calendar widget next button/arrow

cal_next_btn = "//div[@class='container calendar  ']//div[@class=' col auto'][2]"

Code to fetch the text of the month and year and select respective date. If respective month is not available click on the NEXT arrow/btn.

But, here If the desired month and year are not available then code infinite clicking the next arrow/button on the last month and also code could not break/exit from the loop.

        def clickDate(self, expected_month, expected_year, req_day):
            self.events.waitForPresenceOfElement(10, self.journey_date, "xpath")
            self.action.move_to_element(self.events.getelement(self.journey_date, "xpath")).click().perform()
            # self.events.clickElement(self.journey_date, "xpath")
            while True:
                actual_month = self.events.getElementText(self.month_name, "xpath")
                actual_year = self.events.getElementText(self.year_name, "xpath")
                print("Actual Month and Year: ", actual_month, actual_year)
                if (actual_month.casefold() == expected_month.casefold()) and (actual_year == expected_year):
                    all_days_list = self.getElements(self.dates_list, "xpath")
                    for day in all_days_list:
                        print("Day is: " + day.text)
                        print("Day Status is: " + str(day.is_enabled()))
                        try:
                            if day.text.casefold() == req_day.casefold():
                                day.click()
                                break
                        except:
                            print(req_day + ": is not available")
                else:
                    try:
                        self.events.isElementDisplayed(self.cal_next_btn, "xpath")
                        self.events.clickElement(self.cal_next_btn, "xpath")
                    except TimeoutError:
                        print("No More Months Available")
                        break
    

In my logic i'm not able to break/exit from the loop and happening infinite loop when there are no months to check.

Request your help/ideas on this.

Upvotes: 0

Views: 43

Answers (1)

Wonka
Wonka

Reputation: 1901

I solve doing this:

nextpage = True
while nextpage:
    #get data or do what you need to do
    ...

    nextpage = find_element(XPATH_next_page) #return False if not found

Upvotes: 0

Related Questions