【问题标题】:Store a date selected from one datepicker into the next datepicker将从一个日期选择器中选择的日期存储到下一个日期选择器中
【发布时间】:2016-06-02 17:48:10
【问题描述】:

由于找不到current_date = calendar.find_element_by_class_name("ui-datepicker-days-cell-over") 的元素,我在尝试选择返回日期时遇到问题。对于出发日期,它可以工作,但不能返回日期。这是因为当您打开返回日期的日期选择器时,它会自动为您突出显示日期(这是元素),但对于返回日期,打开日期选择器时它没有突出显示日期(仅在您选择日期)。

所以我的问题是有人知道如何修复下面的代码,以便它能够检索从出发日期选择器中选择的日期(选择的日期是 next_available_date)并以某种方式将其存储在返回日期选择器中以便它成为返回日期选择器中的选定日期?

目前下面的代码能够从出发日期选择器检索下一个可用日期没有问题,问题只是返回日期选择器。 (两个代码块几乎是相互镜像的)

# select depart date
datepicker = driver.find_element_by_id("ctl00_centralDynamicContent_OutDateTextBox")
actions.move_to_element(datepicker).click().perform()

# find the calendar, month and year picker and the current date
calendar = driver.find_element_by_id("ui-datepicker-div")
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-days-cell-over")

# 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[@title='Click to see flights on this date' and ancestor::div/@id='ui-datepicker-div']")
    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[@title='Click to see flights on this date']")
                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 return date
datepicker = driver.find_element_by_id("ctl00_centralDynamicContent_InDateTextBox")
actions.move_to_element(datepicker).click().perform()

# find the calendar, month and year picker and the current date
calendar = driver.find_element_by_id("ui-datepicker-div")
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-days-cell-over")

# printing out current date
month = month_picker.first_selected_option.text
year = year_picker.first_selected_option.text
print("Current return 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[@title='Click to see flights on this date' and ancestor::div/@id='ui-datepicker-div']")
    print("Found an available return 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[@title='Click to see flights on this date']")
                print("Found an available return date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year))
                next_available_date.click()
                break
            except NoSuchElementException:
                continue

【问题讨论】:

    标签: python python-2.7 selenium selenium-webdriver


    【解决方案1】:

    因为我记得您正在使用的目标网站,所以这里有一个想法(未经测试)。

    日期选择器将初始日期设置为“只读”日期输入中的内容。让我们移除 readonly 属性并将返回日期输入值设置为之前选择的出发日期:

    from datetime import datetime
    
    # get depart date
    depart_date = datetime.strptime(" ".join([next_available_date.text, month, year]), "%d %b %Y")
    initial_return_date = depart_date.strftime("%d-%m-%Y")
    
    return_date_input = driver.find_element_by_id("ctl00_centralDynamicContent_OutDateTextBox")
    # remove readonly attribute
    driver.execute_script("arguments[0].removeAttribute('readonly','readonly');", return_date_input)
    return_date_input.send_keys(initial_return_date)
    
    # open datepicker
    

    这是一个简单的示例(使用 jet2.com),其中返回日期输入值设置为选择的出发日期:

    from datetime import datetime
    
    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 = "Antalya"
    
    driver = webdriver.Firefox()
    driver.get("http://jet2.com/")
    driver.maximize_window()
    
    wait = WebDriverWait(driver, 90)
    actions = ActionChains(driver)
    
    # wait for the page to load
    wait.until(EC.element_to_be_clickable((By.ID, "return-flight-selector")))
    
    # fill out the form
    return_flight = driver.find_element_by_id('return-flight-selector').click()
    
    depart_from = driver.find_element_by_id("departure-airport-input")
    depart_from.clear()
    depart_from.click()
    depart_from.send_keys(FROM)
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#ui-id-1 .ui-menu-item"))).click()
    
    go_to = driver.find_element_by_id("destination-airport-input")
    go_to.send_keys(TO)
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#ui-id-2 .ui-menu-item"))).click()
    
    # 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-days-cell-over")
    
    # 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[@title='Click to see flights on this date' and ancestor::div/@id='ui-datepicker-div']")
    except NoSuchElementException:
            while True:
                try:
                    calendar.find_element_by_class_name("ui-datepicker-next").click()
                except NoSuchElementException:
                    year_picker = Select(calendar.find_element_by_class_name("ui-datepicker-year"))
                    year_picker.select_by_visible_text(str(int(year.first_selected_option.text) + 1))
    
                try:
                    next_available_date = calendar.find_element_by_xpath(".//td[@title='Click to see flights on this date']")
                    break
                except NoSuchElementException:
                    continue
    
    month_picker = Select(calendar.find_element_by_class_name("ui-datepicker-month"))
    year_picker = Select(calendar.find_element_by_class_name("ui-datepicker-year"))
    
    month = month_picker.first_selected_option.text
    year = year_picker.first_selected_option.text
    
    depart_date = datetime.strptime(" ".join([next_available_date.text, month, year]), "%d %b %Y")
    next_available_date.click()
    
    initial_return_date = depart_date.strftime("%d-%m-%Y")
    
    return_date_input = driver.find_element_by_id("return-date-selector")
    # remove readonly and disabled attributes
    driver.execute_script("arguments[0].removeAttribute('disabled'); arguments[0].removeAttribute('readonly','readonly');", return_date_input)
    
    # set the initial return date
    return_date_input.clear()
    return_date_input.send_keys(initial_return_date)
    
    # open datepicker
    return_date_input.click()
    

    【讨论】:

    • @alexce,它实际上是针对不同的应用程序(技术上相同的应用程序)。我们有不同的测试环境。您使用的环境是过去几周使用的,现在是我们无法脱离工作的测试环境。这两个应用程序之间的唯一区别实际上是第一页,其他所有页面都完全相同。我会给你看这个插件的截图,唯一尴尬的是日历
    • 我将同时显示出发日期和返回日期的 html
    • 根据需要显示出发文本框的图片和html。
    • @BruceyBandit 感谢您提供更多信息。看起来答案可能仍然有效。更新了代码以找到带有id="ctl00_centralDynamicContent_OutDateTextBox"input,我们将使其可编辑并将出发日期发送到。请尝试。
    • 会的。不敢相信今天是周日下午 1 点,并且已经在工作中测试测试用例超过 6 个小时并实现了您一直在帮助我的这些脚本,哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多