如何解决Selenium调用Edge驱动获取performance日志时类型不存在报错
错误原因
该错误是由于Microsoft Edge 96版本对应的EdgeDriver默认未开启性能日志采集,且原有配置方式没有适配Chromium内核Edge的参数命名规则导致的。
修复步骤
- 新增性能日志采集的实验配置,显式开启网络、页面维度的性能日志采集
- 替换日志偏好的Capability键名,Edge使用Chromium内核的参数规则,无法识别
CapabilityType.LOGGING_PREFS对应的默认键名,需要手动指定为goog:loggingPrefs - 校验版本匹配,确保使用的Selenium版本≥3.141.59,且WebDriverManager拉取的edgedriver版本和本地安装的Edge 96.0.1054.34完全匹配
修改后的初始化代码
case "EDGE": System.out.println("Launching Edge Browser"); HashMap<String, Object> EdgePrefs = new HashMap<String, Object>(); EdgePrefs.put("profile.default_content_settings.popups", 0); EdgePrefs.put("download.default_directory", downloadPath); EdgeOptions Edgeoptions = new EdgeOptions(); Edgeoptions.setCapability("prefs", EdgePrefs); Edgeoptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); LoggingPreferences EdgelogPrefs = new LoggingPreferences(); EdgelogPrefs.enable(LogType.PERFORMANCE, Level.ALL); EdgelogPrefs.enable(LogType.DRIVER, Level.ALL); EdgelogPrefs.enable(LogType.BROWSER, Level.ALL); // 新增性能日志实验配置 HashMap<String, Object> perfLoggingPrefs = new HashMap<>(); perfLoggingPrefs.put("enableNetwork", true); perfLoggingPrefs.put("enablePage", true); Edgeoptions.setExperimentalOption("perfLoggingPrefs", perfLoggingPrefs); // 替换为Chromium内核识别的日志偏好键名 Edgeoptions.setCapability("goog:loggingPrefs", EdgelogPrefs); Edgeoptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS,true); WebDriverManager.edgedriver().setup(); driver = new EdgeDriver(Edgeoptions); driver.manage().timeouts().implicitlyWait(TimeOut,TimeUnit.SECONDS); driver.manage().window().maximize(); break;
补充说明
如果修改后还是报错,可以把获取日志的代码里的硬编码字符串替换为枚举类,避免拼写误差:
LogEntries logs = WD.manage().logs().get(LogType.PERFORMANCE);
如果业务允许,升级到Edge 100+版本,对应新EdgeDriver对Selenium参数的兼容性会更好。
内容的提问来源于stack exchange,提问作者NFT Team
相关产品推荐
相关产品推荐

