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

使用Javascript Executor操作SpiceJet日期选择器失败求助

排查SpiceJet网站Selenium日期选择失效问题

问题根源分析

  • 元素定位错误:你当前定位的是一个<div>容器,这类自定义日历的外层容器本身没有value属性,通过JS强行设置value只会修改DOM的属性值,完全不会触发网站内部的日期选择逻辑,也不会同步到实际的表单数据里。
  • 操作逻辑不符合网站交互:现代网站的自定义日历组件几乎都需要模拟真实用户操作——先点击触发按钮打开日历面板,再选择具体日期,而不是直接修改DOM属性。

修正后的实现方案

下面的代码使用显式等待处理页面加载延迟,模拟用户打开日历、选择日期的完整流程:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class SpiceJetCalendarHandler {

    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        try {
            driver.manage().window().maximize();
            driver.manage().deleteAllCookies();
            driver.get("https://www.spicejet.com/");

            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

            // 1. 点击出发日期按钮,打开日历面板
            WebElement dateTrigger = wait.until(ExpectedConditions.elementToBeClickable(
                    By.xpath("//div[text()='Departure Date']/following-sibling::div")
            ));
            dateTrigger.click();

            // 2. 定位目标日期并点击(示例日期:2023年6月6日)
            WebElement targetDate = wait.until(ExpectedConditions.elementToBeClickable(
                    By.xpath("//div[@class='css-1dbjc4n r-1awozwy r-18u37iz r-1wtj0ep']//div[text()='6']")
            ));
            targetDate.click();

            // 验证日期是否设置成功(可选)
            String selectedDate = dateTrigger.getText();
            System.out.println("已选中的日期:" + selectedDate);

            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            driver.quit();
        }
    }
}

额外注意事项

  • 显式等待替代Thread.sleep:尽量用WebDriverWait的显式等待替代固定时长的Thread.sleep,能更稳定地处理页面加载延迟问题。
  • XPath的稳定性:网站的DOM结构可能会更新,若后续XPath失效,需要重新定位元素——可以通过元素的文本、class属性等更稳定的特征来编写定位表达式。
  • 跨月份日期选择:如果需要选择非当前显示月份的日期,还需添加点击日历面板左右箭头切换月份的逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 17:08:13