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

Selenium WebDriver打开浏览器进入系统管理后无法执行后续步骤求助

问题分析与解决方案

核心问题排查

  1. WebDriver实例不共享
    OpenSystemAdmin和TestCaseWithValidDetails各自维护独立的静态WebDriver变量,OpenSystemAdmin初始化的浏览器实例无法被TestCaseWithValidDetails调用,导致后者执行driver.findElement()时触发NullPointerException,若异常未被输出,就会表现为程序停滞。

  2. 元素定位与等待问题
    点击"SYSTEM ADMIN"后页面跳转,若隐式等待未覆盖元素加载时间,或元素定位器(如id("edmail"))与实际页面元素不匹配,WebDriver会一直等待元素出现,造成程序停滞。

  3. ChromeDriver配置错误
    System.setProperty("driverChromeDriver", ".mvn/chromedriver")中的属性名错误,正确应为webdriver.chrome.driver,虽浏览器能打开,但可能引发后续操作异常。


具体修复方案

1. 统一WebDriver实例传递

通过参数传递共享浏览器实例,避免多类维护独立静态变量:

Main类修改

public static void main(String[] args) {
    WebDriver driver = openWeb();
    testCase001(driver);
}

// 返回初始化好的浏览器实例
public static WebDriver openWeb() {
    OpenSystemAdmin open = new OpenSystemAdmin();
    open.openWeb();
    open.pressAdminLoginButton();
    return OpenSystemAdmin.driver;
}

// 接收浏览器实例并传入测试方法
public static void testCase001(WebDriver driver) {
    TestCaseWithValidDetails testCase = new TestCaseWithValidDetails();
    testCase.testCaseWithValidDetails1(driver);
}

TestCaseWithValidDetails类修改
移除静态driver变量,改为方法参数接收:

public class TestCaseWithValidDetails {

    //TestCase ID : LogIn_001
    //Test Description : Verify that the logIn function works with the right details.
    public void testCaseWithValidDetails1(WebDriver driver) {
        // 等待用户名输入框加载完成(显式等待更可靠)
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("edmail")));
        username.sendKeys("email");

        // 密码输入框
        WebElement password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
        password.sendKeys("password");

        // 调用登录按钮方法
        OpenSystemAdmin.enterButton(driver);

        // 恢复截图与结果输出
        try {
            Thread.sleep(2000);
            screenShot(driver, "Screenshots_LogIn.Passes//" + screenShotNames());
            screenShot(driver, "Screenshots_LogIn.Passes//" + screenShotNames());
            System.out.println("Login in test passed");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

OpenSystemAdmin类修改
修正ChromeDriver配置,并让登录按钮方法接收driver参数:

public class OpenSystemAdmin {

    public static WebDriver driver;

    public static void openWeb() {
        try {
            // 修正ChromeDriver属性名
            System.setProperty("webdriver.chrome.driver", ".mvn/chromedriver");
            driver = new ChromeDriver();
            driver.manage().deleteAllCookies();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
            driver.get("https://mrflutter.000webhostapp.com/");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void pressAdminLoginButton() {
        WebElement pressSystemAdmin = driver.findElement(By.linkText("SYSTEM ADMIN"));
        pressSystemAdmin.click();
    }

    // 修改为接收WebDriver参数
    public static void enterButton(WebDriver driver) {
        driver.findElement(By.cssSelector("button[type='submit']")).click();
    }
}

2. 优化等待逻辑

用显式等待替代单一隐式等待,针对每个元素单独设置等待条件,避免不必要的停滞,同时确保元素加载完成后再执行操作。

3. 验证定位器正确性

打开登录页面后,用浏览器开发者工具检查用户名、密码输入框的实际id是否与代码中的edmail、email一致,若不一致需修正定位器。


调试技巧

  • 在关键步骤添加System.out.println()输出,比如进入testCase001、开始查找元素等,确认程序执行到哪一步停滞。
  • 查看控制台异常信息,NullPointerException或TimeoutException会直接提示问题根源。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 10:45:06