2016-02-07 48 views
2

我遇到了一個我似乎無法理解的python問題。不知道我是否需要使用if語句,但是因爲我是python新手,我實際上並不確定如何編寫這個小問題。如何查看當前日期並移至下一日期

事實上,這是我的問題。對於出發日歷,我希望python能夠執行以下操作:

  • 查看「您的日期」。如果有航班(低價或正常無關緊要),請點擊它。如果沒有,則轉到下一個有空位的日期,然後點擊該日期。
  • 如果在當月沒有可用的日期,我需要能夠移動到下個月(我有一個示例代碼)。

對於返回日曆,我希望它做同樣的事情,但要確保它在選擇的出發日期後至少7天選擇一個日期。

這幾乎是我的問題,該怎麼做?

下面是depature日曆的HTML(回曆是完全一樣的,除了它是inboundsearchresults而不是對外的搜索結果):

下面我有一個示例代碼從一個普通的日期選擇器選擇時的工作(這在頁面中使用的URL之前),如果你想使用模板和操縱它:

# select depart 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 departure date: {day} {month} {year}".format(day=current_date.text, month=month, year=year)) 

# 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 departure 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 departure date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year)) 
       next_available_date.click() 
       break 
      except NoSuchElementException: 
       continue 

回答

1

的想法是定義一個可重複使用的功能 - 調用它select_date()接收一個「日曆」 WebElement和一個可選的最低日期。這個函數首先會在日曆中尋找Your date,如果它在那裏並且超過最小值(如果有的話)點擊它並返回日期。如果沒有Your date,請查找可用的「航班」天數,如果給出了最短日期並且日期大於或等於此日期,請單擊它並返回日期。

工作實現:

from datetime import datetime, timedelta 

from selenium import webdriver 
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 


def select_date(calendar, mininum_date=None): 
    try: 
     # check if "Your Date" is there 
     your_date_elm = calendar.find_element_by_class_name("your-date") 

     your_date = your_date_elm.get_attribute("data-date") 
     print("Found 'Your Date': " + your_date) 
     your_date_elm.click() 

     # check if your_date against the minimum date if given 
     your_date = datetime.strptime(your_date, "%Y-%m-%d") 
     if mininum_date and your_date < mininum_date: 
      raise NoSuchElementException("Minimum date violation") 
     return your_date 
    except NoSuchElementException: 
     flight_date = None 
     flight_date_elm = None 
     while True: 
      print("Processing " + calendar.find_element_by_css_selector("div.subheader > p").text) 

      try: 
       if mininum_date: 
        flight_date_elms = calendar.find_elements_by_class_name("flights") 
        flight_date_elm = next(flight_date_elm for flight_date_elm in flight_date_elms 
              if datetime.strptime(flight_date_elm.get_attribute("data-date"), "%Y-%m-%d") >= mininum_date) 
       else: 
        flight_date_elm = calendar.find_element_by_class_name("flights") 
      except (StopIteration, NoSuchElementException): 
       calendar.find_element_by_partial_link_text("Next month").click() 

      # if found - print out the date, click and exit the loop 
      if flight_date_elm: 
       flight_date = flight_date_elm.get_attribute("data-date") 
       print("Found 'Flight Date': " + flight_date) 
       flight_date_elm.click() 
       break 

     return datetime.strptime(flight_date, "%Y-%m-%d") 


driver = webdriver.Firefox() 
driver.get("http://www.jet2.com/cheap-flights/leeds-bradford/antalya/2016-03-01/2016-04-12?adults=2&children=2&infants=1&childages=4%2c6") 

wait = WebDriverWait(driver, 10) 

# get the outbound date 
outbound = wait.until(EC.visibility_of_element_located((By.ID, "outboundsearchresults"))) 
outbound_date = select_date(outbound) 

# get the inbound date 
inbound = driver.find_element_by_id("inboundsearchresults") 
inbound_minimum_date = outbound_date + timedelta(days=7) 
inbound_date = select_date(inbound, mininum_date=inbound_minimum_date) 

print(outbound_date, inbound_date) 

driver.close() 

對於在問題URL提供,它打印:

Processing March 2016 
Found 'Flight Date': 2016-03-28 

Processing April 2016 
Found 'Flight Date': 2016-04-04 

2016-03-28 00:00:00 2016-04-04 00:00:00 

末印刷的兩個日期是出發和返回日期。

讓我知道你是否需要任何澄清,並希望它有幫助。

+0

完美,我有很多東西要在短時間內學習,但是用你的腳本,我可以用它來幫助操縱我擁有的其他腳本。後來還有一個障礙需要處理,可能需要稍後再請求你的幫助,但同時,非常感謝你:) – BruceyBandit

+0

@BruceyBandit很高興看到你正在進行更深入的測試自動化。讓我知道你是否需要進一步的幫助。 – alecxe

相關問題