Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查当前日期并移动到下一个日期_Python_Python 2.7_Selenium_Selenium Webdriver - Fatal编程技术网

Python 如何检查当前日期并移动到下一个日期

Python 如何检查当前日期并移动到下一个日期,python,python-2.7,selenium,selenium-webdriver,Python,Python 2.7,Selenium,Selenium Webdriver,我有一个python问题,我似乎无法理解。我不确定是否需要使用if语句,但因为我是python新手,所以我不确定如何编写这个小问题 事实上,这就是我面临的问题。对于出发日历,我希望python能够执行以下操作: 查看“你的约会”。如果有航班(无论是低票价还是普通票价),点击它。如果没有,则转到下一个有航班的可用日期并单击该日期 如果当前月份没有可用的日期,则需要能够移动到下个月(我有一个示例代码) 对于回程日历,我希望它做同样的事情,但确保它选择的日期至少比所选的出发日期晚7天 这实际上是我

我有一个python问题,我似乎无法理解。我不确定是否需要使用if语句,但因为我是python新手,所以我不确定如何编写这个小问题

事实上,这就是我面临的问题。对于出发日历,我希望python能够执行以下操作:

  • 查看“你的约会”。如果有航班(无论是低票价还是普通票价),点击它。如果没有,则转到下一个有航班的可用日期并单击该日期
  • 如果当前月份没有可用的日期,则需要能够移动到下个月(我有一个示例代码)
对于回程日历,我希望它做同样的事情,但确保它选择的日期至少比所选的出发日期晚7天

这实际上是我的问题,怎么做

下面是Deputure日历的html(返回日历与此完全相同,只是它是inboundsearchresults,而不是outbound SearchResults):

下面我有一个示例代码,当您想使用该模板并对其进行操作时,从普通的日期选择器(这在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

其思想是定义一个可重用的函数——调用它
select_date()
,该函数接收一个“日历”WebElement和一个可选的最小日期。此函数将首先在日历中查找
您的日期
,如果该日期在日历中且超过最小值(如果给定),请单击该日期并返回日期。如果没有
您的日期
,请查找可用的“航班”天数,如果给出了最短日期且日期大于或等于该日期,请单击该日期并返回日期

工作执行:

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()
最后打印的两个日期是出发日期和返回日期


如果您需要任何澄清,请告诉我,并希望它能有所帮助。

很好,我在短时间内有很多东西要学习,但有了您的脚本,我可以用它来帮助操作其他脚本。还有一个障碍我以后要处理,可能以后需要寻求您的帮助,但同时,非常感谢:)@bruceyband我很高兴看到您正在深入测试自动化。如果你需要进一步的帮助,请告诉我。
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