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

Selenium DevTools中如何获取responseReceived()的精确时间戳

提取Selenium DevTools中响应被接收的精确时间戳

在使用Chrome DevTools的Network.responseReceived()监听器时,你可以直接通过response.getTimestamp()获取浏览器接收该响应的精确时间戳,这个时间戳和请求的getWallTime()格式一致,都是以秒为单位的Unix时间戳(包含小数,精确到毫秒级别)。

示例代码调整:

devTools.addListener(Network.responseReceived(), response -> {
    if (response.getType().toString().equalsIgnoreCase("xhr")) {
        // 获取响应被浏览器接收的时间戳
        double receiveTimestamp = response.getTimestamp();
        // 转换为Java Instant对象,方便后续时间处理
        Instant receiveTime = Instant.ofEpochSecond(
            (long) receiveTimestamp,
            (long) ((receiveTimestamp - (long) receiveTimestamp) * 1_000_000_000)
        );
        
        // 补充:你提到的responseTime是服务器生成响应的时间,并非浏览器接收时间
        double serverResponseTimestamp = response.getResponse().getResponseTime();
        Instant serverResponseTime = Instant.ofEpochSecond(
            (long) serverResponseTimestamp,
            (long) ((serverResponseTimestamp - (long) serverResponseTimestamp) * 1_000_000_000)
        );
        
        // 后续业务逻辑处理
    }
});

关键说明:

  • response.getTimestamp():对应Chrome DevTools Protocol中Network.responseReceived事件的timestamp字段,代表浏览器完成响应接收的时间点,这正是你需要的精确接收时间戳。
  • response.getResponse().getResponseTime():对应HTTP响应头的Date字段,是服务器端生成响应的时间,和浏览器接收响应的时间存在网络传输耗时差,不能作为接收时间使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 08:24:52