java 使用 selenium 报错:InvalidArgument invalid argument: log type ‘performance‘ not found

6 篇文章 0 订阅

新版本需要增加以下配置(我使用的是 4.23.0 版本)

       // 创建 ChromeOptions 实例
        ChromeOptions options = new ChromeOptions();
        // 设置性能日志级别为 ALL
        options.setCapability("goog:loggingPrefs", ImmutableMap.of("performance", "ALL"));

        WebDriver driver = new ChromeDriver(options);

伪代码

    /**
     * 等待接口执行完成
     * @param path 接口路径
     */
    private static void waiteApi(WebDriver driver, String path) {
        // 定义一个预期条件来等待 AJAX 请求完成
        // 使用 WebDriverWait 等待 AJAX 请求完成
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); // 最长等待时间为 10 秒
        wait.until(webDriver -> {
            LogEntries performanceLogs = webDriver.manage().logs().get(LogType.PERFORMANCE);
            List<LogEntry> entries = performanceLogs.getAll();

            for (LogEntry entry : entries) {
                String message = entry.getMessage();
                if (message.contains("Network.responseReceived") && message.contains(path)) { // 假设接口路径为 /api/some-endpoint
                    return true;
                }
            }

            return false;
        });
        // 在这里执行其他操作
        System.out.println("AJAX request has completed.");
    }

    public static void main(String[] args) {
        // 创建 ChromeOptions 实例
        ChromeOptions options = new ChromeOptions();
        // 设置性能日志级别为 ALL
        options.setCapability("goog:loggingPrefs", ImmutableMap.of("performance", "ALL"));

        WebDriver driver = new ChromeDriver(options);
        WebElement loginButton = driver.findElement(By.className("login-button"));
        //登录
        loginButton.click();
        waiteApi(driver, "user/login");
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值