You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Selenium WebDriver遍历Omio网站日期选择器的日期

解决Omio日期选择器操作问题

问题分析

原代码存在两个核心问题:

  • 仅等待元素存在而非可点击状态,导致元素未加载完成时点击无效
  • 未确保日期选择框已完全展开,直接定位元素可能触发上下文错误

修正后的实现方案

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

# 初始化浏览器与等待器
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 15)
driver.get("https://www.omio.com/")

# 1. 先触发日期选择框展开(必须步骤)
departure_input = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[data-cy="departure-date-input"]')))
departure_input.click()

# 2. 等待当前选中日期处于可交互状态
current_day = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'li[data-cy="firstSelectedDay"]')))

# 3. 定位并等待下一个可用日期元素可点击
next_day = wait.until(EC.element_to_be_clickable(
    (By.XPATH, './following-sibling::li[@data-e2e="calendarDay" and not(@data-disabled-cy="true")]')
))

# 4. 用动作链模拟点击,避免元素不可见或被遮挡的问题
ActionChains(driver).move_to_element(next_day).click().perform()

# 后续操作逻辑...

额外排查要点

  • 若仍失效,先检查页面是否有弹窗遮挡日期选择器,需先关闭弹窗
  • 验证data-disabled-cy属性值:部分场景下该属性可能为布尔值而非字符串,需调整XPath判断逻辑
  • 必要时可添加短暂隐式等待(driver.implicitly_wait(2)),但优先使用显式等待

内容的提问来源于stack exchange,提问作者marwan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 07:07:35