由于无法找到current_date = calendar.find_element_by_class_name(“ui-datepicker-days-cell-over”)的元素,因此我在尝试选择返回日期时遇到问题.对于离开日期,它有效但不是返回日期.这是因为当您打开返回日期的日期选择器时,它会自动为您突出显示一个日期(这是元素),但是对于返回日期,它的日期选择器打开时没有突出显示日期(仅在您选择之后)一个约会).
所以我的问题是有没有人知道如何修复下面的代码,以便它能够检索从离开日期选择器中选择的日期(选择的日期是next_available_date)并以某种方式将其存储在返回日期选择器中,以便它成为一个选定的返回datepicker中的日期?
目前,下面的代码能够从离开日期选择器中检索下一个可用日期没有问题,问题只是返回datepicker. (两个代码块都相互镜像)
# 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
最佳答案 因为我记得你正在使用的目标网站,这是一个想法(未经过测试).
datepicker将初始日期设置为“readonly”日期输入中的内容.让我们删除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()