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

Makemytrip网站出发日期点击失败,抛出Selenium超时异常求助

Selenium操作Makemytrip出发日期栏异常解决

问题场景

使用Selenium自动化操作Makemytrip网站时,尝试点击出发日期字段中的特定日期,程序持续抛出异常,核心错误为找不到指定元素。

错误信息

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.xpath: //div[@class='flt_fsw_inputBox dates inactiveWidget ']//span[contains(text(), 'Departure')] (tried for 20 second(s) with 500 milliseconds interval)
        at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:84)
        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:230)
        at demo.TestCases.clickOnDeparture(TestCases.java:123)
        at demo.TestCases.testCase02(TestCases.java:98)
        at demo.App.getGreeting(App.java:17)
        at demo.App.main(App.java:26)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='flt_fsw_inputBox dates inactiveWidget ']//span[contains(text(), 'Departure')]"}
  (Session info: chrome=121.0.6167.85)
Build info: version: '4.14.0', revision: 'e5a96349ce'
System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '14.0', java.version: '17.0.9'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [a7605f56c923ab8911d2a39e2a4aeedc, findElement {using=xpath, value=//div[@class='flt_fsw_inputBox dates inactiveWidget ']//span[contains(text(), 'Departure')]}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 121.0.6167.85, chrome: {chromedriverVersion: 120.0.6099.109 (3419140ab66..., userDataDir: /var/folders/vs/y2kphm_x7bj...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:50083}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: mac, proxy: Proxy(), se:cdp: ws://localhost:50083/devtoo..., se:cdpVersion: 121.0.6167.85, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: a7605f56c923ab8911d2a39e2a4aeedc
        at jdk.internal.reflect.GeneratedConstructorAccessor8.newInstance(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:52)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:191)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:196)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:171)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:602)
        at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:165)
        at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:59)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:359)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:353)
        at org.openqa.selenium.support.ui.ExpectedConditions$6.apply(ExpectedConditions.java:171)
        at org.openqa.selenium.support.ui.ExpectedConditions$6.apply(ExpectedConditions.java:168)
        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:205)
        ... 4 more

> Task :run FAILED

FAILURE: Build failed with an exception.

解决方案

  • 优化元素定位器:Makemytrip页面元素的class属性(如inactiveWidget)可能随页面状态动态变化,避免依赖完整静态class值。改用更稳定的XPath定位:
    //span[contains(text(), 'Departure')]/ancestor::div[contains(@class, 'flt_fsw_inputBox')]
    
  • 等待元素可交互:替换presenceOfElementLocated()为elementToBeClickable(),确保元素不仅存在且可点击:
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
    WebElement departureField = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(text(), 'Departure')]")));
    departureField.click();
    
  • 处理页面弹窗:首页的广告弹窗会遮挡目标元素,需先定位并关闭弹窗再操作日期栏。
  • 匹配ChromeDriver版本:当前Chrome浏览器版本为121.0.6167.85,但ChromeDriver版本为120.0.6099.109,版本不匹配会导致元素定位异常,需更新ChromeDriver至对应版本。
  • 避免等待方式混用:不要同时使用隐式等待和显式等待,确保等待逻辑仅针对目标元素,避免冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 18:02:03