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

Java Selenium使用RemoteWebDriver无法启用性能日志报错如何解决

问题原因
  • 你使用的Chrome 95版本默认启用W3C通信协议,RemoteWebDriver与本地ChromeDriver的Capability传递逻辑存在差异:本地ChromeDriver可以直接识别旧版CapabilityType.LOGGING_PREFS配置,但RemoteWebDriver和远端节点通信时,旧版无前缀的loggingPrefs配置不会被映射为Chrome识别的W3C标准参数goog:loggingPrefs,导致远端ChromeDriver未开启性能日志收集,访问时就会抛出日志类型不存在的错误。
  • 你当前将ChromeOptions作为子Capability塞入DesiredCapabilities的写法在新版Selenium中已不推荐,容易导致部分配置在传递过程中丢失。
解决方案

推荐直接在ChromeOptions中配置W3C兼容的日志偏好,无需单独创建DesiredCapabilities合并配置,修改后的代码如下:

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.RemoteWebDriver;

public WebDriver getDriver() throws MalformedURLException {
  ChromeOptions chromeOptions = new ChromeOptions();
  chromeOptions.addArguments(arguments);
  chromeOptions.setAcceptInsecureCerts(acceptInsecureCerts);

  // 配置W3C协议兼容的性能日志参数
  LoggingPreferences logPrefs = new LoggingPreferences();
  logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
  chromeOptions.setCapability("goog:loggingPrefs", logPrefs);

  // 直接传入ChromeOptions初始化RemoteWebDriver即可
  return new RemoteWebDriver(new URL(url), chromeOptions);
}

如果你的Selenium版本较低必须使用DesiredCapabilities,则需要将日志配置的键名修改为goog:loggingPrefs,示例如下:

DesiredCapabilities desiredCapabilites = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
// 注意这里用带goog前缀的键名,而非CapabilityType.LOGGING_PREFS
desiredCapabilites.setCapability("goog:loggingPrefs", logPrefs);
desiredCapabilites.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

修改完成后可先调用driver.manage().logs().getAvailableLogTypes()打印所有支持的日志类型,确认performance已出现在列表中即可正常获取性能日志。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 09:15:04