gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:10 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com