python - 将从一个日期选择器中选择的日期存储到下一个日期选择器中

标签 python python-2.7 selenium selenium-webdriver

由于无法找到 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

最佳答案

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

日期选择器将初始日期设置为“只读”日期输入中的日期。让我们删除 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()

关于python - 将从一个日期选择器中选择的日期存储到下一个日期选择器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35534369/

相关文章:

python - 如何在 matplotlib 颜色条中创建自定义断点?

javascript - 使用 Selenium Webdriver 获取定期更新的内容

java - 如何使用 Selenium 向下滚动?

java - Selenium 3.7.1(Java)和 ChromeDriver 2.33.5

python - 如何点击img元素?

python - 如何从python中的N个不同正态分布中采样M次?在处理时间方面有没有 "faster"方式?

Python 将字典列表转换为另一个字典列表

python - 分组匹配错误,django url dispatcher

python - 使用Python从MS Access中提取数据

python-2.7 - 在 matplotlib 散点图中设置 xticks 的位置