Automating such a task in the Selenium IDE would be quite difficult, since there is a nontrivial logic associated with getting the next available date, you should consider switching to Selenium WebDriver choosing one of the available language bindings of selenium.
Here is the working code made using Pelon language Selenium bindings . The idea is as follows:
- create an instance of WebDriver (using
Firefox()
in this example, but there are other options) - enlarge browser window
- go to url ( http://www.jet2.com )
- Wait for the page to load explicitly ( docs )
- fill in the departure and destination fields
- click the Depart field to start the calendar.
- in the calendar, find the current date - it has a class
ui-datepicker-current-day
- check if there is an available date this month using the XPath location technique and the
following
axis . If yes, print and press. - if we did not find the next available date in this month, we also initialize the “endless” cycle and click the “Next” month button, checking for the availability of the date. Print it and click if it is found and exits the loop. Otherwise, click Next.
- if we don’t have the Next button, then we will end the year, select the next year from the drop-down list, continue the cycle
- close the driver upon completion
The code:
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.support.select import Select from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC FROM = "Leeds Bradford" TO = "Budapest BUD" driver = webdriver.Firefox() # or, webdriver.Chrome(), or webdriver.PhantomJS() or etc. driver.maximize_window() driver.get("http://www.jet2.com") wait = WebDriverWait(driver, 10) actions = ActionChains(driver) # wait for the page to load wait.until(EC.presence_of_element_located((By.ID, "departure-airport-input"))) # fill out the form driver.find_element_by_id("departure-airport-input").send_keys(FROM) wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#ui-id-1 .ui-menu-item"))).click() driver.find_element_by_id("destination-airport-input").send_keys(TO) wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#ui-id-2 .ui-menu-item"))).click() # select date datepicker = driver.find_element_by_id("departure-date-selector") actions.move_to_element(datepicker).click().perform() # find the calendar, month and year picker and the current date calendar = driver.find_element_by_id("departureDateContainer") month_picker = Select(calendar.find_element_by_class_name("ui-datepicker-month")) year_picker = Select(calendar.find_element_by_class_name("ui-datepicker-year")) current_date = calendar.find_element_by_class_name("ui-datepicker-current-day") # printing out current date month = month_picker.first_selected_option.text year = year_picker.first_selected_option.text print("Current date: {day} {month} {year}".format(day=current_date.text, month=month, year=year)) try: # see if we have an available date in this month 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 driver.close()
Test results:
Leeds Bradford → Antalya AYT (next available date in April):
Current date: 28 Mar 2016 Processing Apr 2016 Found an available date: 4 Apr 2016
Leeds Bradford → Budapest BUD (the next available date in the same month as the current date):
Current date: 12 Feb 2016 Found an available date: 15 Feb 2016
? (next available date next year)
alecxe
source share