我正在尝试使用 Python 3.12.3 中的 Selenium 操作 Omio 上的日期选择器。我希望从今天开始,然后选择第二天,依此类推。
不幸的是,出发日期字段是只读的,因此不能使用
send_keys
。
我尝试找到当前选定的日期,然后使用该日期查找第二天,但 WebDriver 只是关闭,而不是单击第二天。我还考虑过使用箭头键移至第二天,但该网站不接受它们作为日期选择器的输入。
current_day = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'li[data-cy="firstSelectedDay"]')))
next_day = current_day.find_element(By.XPATH, 'following-sibling::li[@data-e2e="calendarDay" and @data-disabled-cy="false"]')
next_day.click()
检查下面的代码并解释如何在出发日期选择器中选择当前日期并在返回日期选择器中选择当前日期+1:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.omio.com/")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
# Click on Accept all cookies button(if this pop-up doesn't appear on your screen then delete this line of code)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept all']"))).click()
# Click on Departure Date picker
wait.until(EC.element_to_be_clickable((By.ID, "buttonDepartureDate"))).click()
# Click on current day
wait.until(EC.element_to_be_clickable((By.XPATH, "(//li[@data-disabled-cy='false'])[1]"))).click()
# Click on Return Date picker
wait.until(EC.element_to_be_clickable((By.ID, "buttonReturnDate"))).click()
# Click on current day + 1 (if you want current day + 2, just change the value in the below XPath to [3])
wait.until(EC.element_to_be_clickable((By.XPATH, "(//li[@data-disabled-cy='false'])[2]"))).click()
time.sleep(15)