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

Edge自动化测试遇不安全弹窗+NoSuchElementException问题求助

问题描述
  • 执行Selenium EdgeDriver+Cucumber脚本时,打开目标URL后会弹出多余的不安全网页窗口
  • 已确认元素定位器正确,但仍抛出NoSuchElementException异常,尝试过窗口切换、重新导航等方法均无效

脚本功能:打开浏览器访问OpenCart演示页面,执行元素点击操作。

原脚本代码

package StepDefinitions;

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

import io.cucumber.java.Before;
import io.cucumber.java.en.*;

public class OpencartDemoNavigation {
    
    private WebDriver driver;
    
    @Before
    public void setup()
    {
    // Setting up things for successful navigation to unsecured websites
    EdgeOptions options = new EdgeOptions();
    options.setAcceptInsecureCerts(true);
    options.addArguments("--ignore-certificate-errors");
    

    // Declaring webdriver element
    driver = new EdgeDriver(options);
    }
    

    @Given("chrome browser is open")
    public void chrome_browser_is_open() {
        System.out.println("Inside Step - Chrome browser is open");

        String projectPath = System.getProperty("user.dir");
        System.out.println("The current project path is: " + projectPath);

        System.setProperty("webdriver.edge.driver", projectPath + "/src/test/resources/Drivers/msedgedriver.exe");
        driver.manage().window().maximize();

    }

    @When("user enters opencart demo URL in the chrome address bar and hits enter")
    public void user_enters_opencart_demo_url_in_the_chrome_address_bar_and_hits_enter() throws InterruptedException {
        System.out.println("Inside Step - User enters opencart demo URL in the chrome address bar and hits enter");

        driver.get("https://www.opencart.com/index.php?route=cms/demo");
        
        Thread.sleep(15000);
    }

    @When("user clicks opencart demo storefront tab")
    public void user_clicks_opencart_demo_storefront_tab() throws InterruptedException {
        System.out.println("Inside Step - User clicks opencart demo storefront tab");
        driver.findElement(By.xpath("//a[text()='Features']/ancestor::nav/..//div[contains(@class,'container')]/..//a[text()='Features']")).click();
    }
}

抛出异常

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//a[text()='Features']/ancestor::nav/..//div[contains(@class,'container')]/..//a[text()='Features']"}

解决方案

问题1:多余不安全窗口弹出

原因

  • 驱动路径设置时机错误:System.setProperty放在Given步骤中,导致Driver初始化时未正确加载驱动配置,触发异常跳转
  • EdgeOptions参数不完整,未阻止弹窗和证书相关的默认提示

修复

  1. 将System.setProperty移至setup()方法最开头,确保驱动加载完成后再初始化Driver
  2. 补充EdgeOptions参数,阻止弹窗并强化证书错误忽略:
    options.addArguments("--disable-popup-blocking");
    options.addArguments("--ignore-certificate-errors-spki-list");
    options.addArguments("--start-maximized"); // 直接在配置中设置窗口最大化
    

问题2:NoSuchElementException异常

原因

  • XPath定位器过于冗余,易因页面结构微小变化失效
  • 用Thread.sleep做固定等待,无法保证元素完全渲染完成
  • 多余窗口导致Driver焦点不在目标页面

修复

  1. 简化XPath定位器:直接定位目标元素,比如//a[text()='Features']或更稳定的属性定位//a[@href='https://www.opencart.com/features']
  2. 改用显式等待替代固定休眠,确保元素可点击后再操作:
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Features']"))).click();
    
  3. 打开URL后自动关闭多余窗口,切换到目标页面:
    Set<String> handles = driver.getWindowHandles();
    if (handles.size() > 1) {
        for (String handle : handles) {
            driver.switchTo().window(handle);
            if (driver.getCurrentUrl().contains("opencart.com")) {
                break;
            } else {
                driver.close();
            }
        }
    }
    

修正后的完整脚本
package StepDefinitions;

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;
import java.util.Set;

import io.cucumber.java.Before;
import io.cucumber.java.en.*;

public class OpencartDemoNavigation {
    
    private WebDriver driver;
    
    @Before
    public void setup()
    {
        // 先加载驱动路径
        String projectPath = System.getProperty("user.dir");
        System.setProperty("webdriver.edge.driver", projectPath + "/src/test/resources/Drivers/msedgedriver.exe");
        
        // 配置Edge浏览器参数
        EdgeOptions options = new EdgeOptions();
        options.setAcceptInsecureCerts(true);
        options.addArguments("--ignore-certificate-errors");
        options.addArguments("--ignore-certificate-errors-spki-list");
        options.addArguments("--disable-popup-blocking");
        options.addArguments("--start-maximized");
        
        // 初始化Driver
        driver = new EdgeDriver(options);
    }
    
    @Given("edge browser is open")
    public void edge_browser_is_open() {
        System.out.println("Inside Step - Edge browser is open");
        System.out.println("The current project path is: " + System.getProperty("user.dir"));
    }

    @When("user enters opencart demo URL in the edge address bar and hits enter")
    public void user_enters_opencart_demo_url_in_the_edge_address_bar_and_hits_enter() {
        System.out.println("Inside Step - User enters opencart demo URL in the edge address bar and hits enter");

        driver.get("https://www.opencart.com/index.php?route=cms/demo");
        
        // 关闭多余窗口,聚焦目标页面
        Set<String> handles = driver.getWindowHandles();
        if (handles.size() > 1) {
            for (String handle : handles) {
                driver.switchTo().window(handle);
                if (driver.getCurrentUrl().contains("opencart.com")) {
                    break;
                } else {
                    driver.close();
                }
            }
        }
    }

    @When("user clicks opencart demo features tab")
    public void user_clicks_opencart_demo_features_tab() {
        System.out.println("Inside Step - User clicks opencart demo features tab");

        // 显式等待元素可点击后执行点击
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Features']"))).click();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 14:06:03