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

MakeMyTrip登录弹窗及广告无法关闭阻碍首页访问,自动化测试求助

解决MakeMyTrip官网弹窗与登录窗口无法关闭的Selenium方案

原代码存在的问题

  • 广告拦截参数无效:--disable-extensions无法有效拦截网站原生弹窗广告
  • 未处理登录弹窗:仅针对广告弹窗编写逻辑,遗漏了登录窗口的关闭处理
  • 语法错误:adds. Click()存在空格,应为adds.click()
  • 测试用例顺序不合理:先点击logo再处理弹窗,逻辑倒置
  • WebDriverWait重复定义,代码冗余

修正后的完整代码

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class MakeMyTripTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        // 配置Edge浏览器选项,减少弹窗和广告触发
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--disable-notifications"); // 禁用通知弹窗
        options.addArguments("--disable-popup-blocking"); // 允许弹窗以便后续处理
        options.addArguments("--incognito"); // 启用无痕模式,避免缓存影响
        options.addArguments("--blink-settings=imagesEnabled=false"); // 禁用图片加载,减少广告加载

        driver = new EdgeDriver(options);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        wait = new WebDriverWait(driver, Duration.ofSeconds(15)); // 全局显式等待
        driver.get("https://www.makemytrip.com/");
    }

    @AfterClass
    public void tearDown() {
        System.out.println("测试结束,关闭浏览器");
        if (driver != null) {
            driver.quit();
        }
    }

    // 通用点击方法,确保元素可点击后执行操作
    private void clickElement(By locator) {
        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(locator));
        Actions action = new Actions(driver);
        action.moveToElement(element).click().build().perform();
    }

    // 关闭登录弹窗
    private void closeLoginModal() {
        try {
            By loginCloseBtn = By.xpath("//span[@data-cy='closeModal']");
            clickElement(loginCloseBtn);
            System.out.println("登录弹窗已关闭");
        } catch (Exception e) {
            System.out.println("未检测到登录弹窗或关闭失败:" + e.getMessage());
        }
    }

    // 关闭广告弹窗
    private void closeAdPopup() {
        try {
            By adCloseBtn = By.xpath("//i[@class='wewidgeticon we_close']");
            clickElement(adCloseBtn);
            System.out.println("广告弹窗已关闭");
        } catch (Exception e) {
            System.out.println("未检测到广告弹窗或关闭失败:" + e.getMessage());
        }
    }

    @Test(priority = 1)
    public void handleAllPopups() {
        // 先处理登录弹窗,再处理广告弹窗
        closeLoginModal();
        closeAdPopup();
    }

    @Test(priority = 2)
    public void navigateToHomePage() {
        clickElement(By.xpath("//a[@data-cy='mmtLogo']"));
        System.out.println("已成功进入首页");
    }
}

关键优化点说明

  1. 浏览器参数优化:通过--incognito、--disable-notifications等参数减少弹窗触发概率,提升测试稳定性
  2. 全局显式等待:仅初始化一次WebDriverWait,避免重复定义,简化代码结构
  3. 弹窗容错处理:为每个弹窗关闭逻辑添加异常捕获,避免因弹窗未出现导致测试中断
  4. 测试逻辑调整:先完成所有弹窗关闭操作,再执行首页导航,符合正常用户操作流程
  5. 通用点击封装:确保元素处于可点击状态后再执行点击,解决元素未加载完成导致的点击失败问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 10:35:32