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

自动化MakeMyTrip时无法点击弹窗外部关闭登录弹窗问题

解决MakeMyTrip登录弹窗的自动化关闭问题

你当前代码尝试点击主页轮播图区域绕过弹窗,但这类登录模态弹窗是层级更高的遮罩元素,点击下层主页元素会被弹窗拦截,根本无法触发关闭动作。正确做法是直接定位弹窗的关闭按钮并执行点击操作。

解决方案步骤

  • 定位弹窗的关闭按钮:通过浏览器开发者工具(F12)找到弹窗右上角的叉号按钮,通常带有commonModal__close、closeIcon这类标识性class;
  • 用显式等待替代Thread.sleep:确保弹窗加载完成后再操作,避免因页面加载延迟导致元素定位失败;
  • 优先使用普通点击,若遇到元素不可点击的情况,再用JavaScript辅助点击。

修改后的代码示例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

// 定义登录弹窗关闭按钮的定位器(可根据页面实际元素调整)
private By loginPopupCloseBtn = By.xpath("//span[@class='commonModal__close']");

public void setInputEmail(String username, String password) {
    driver.get("https://www.makemytrip.com/");
    
    // 显式等待弹窗关闭按钮可点击,最长等待10秒
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    WebElement closeBtn = wait.until(ExpectedConditions.elementToBeClickable(loginPopupCloseBtn));
    
    // 点击关闭弹窗
    closeBtn.click();

    // 若普通点击无效,可替换为JavaScript点击
    // JavascriptExecutor js = (JavascriptExecutor) driver;
    // js.executeScript("arguments[0].click();", closeBtn);
    
    // 关闭弹窗后,即可执行航班搜索相关操作
    // ... 你的航班搜索代码逻辑
}

注意事项

如果上述XPath定位不到元素,打开浏览器开发者工具,查看关闭按钮的真实属性(比如class、id),调整定位器。例如部分场景下关闭按钮的class为closeIcon,对应的XPath可改为//div[@class='closeIcon']。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 18:32:49