- // 启用性能日志跟踪
- ChromeOptions options = new ChromeOptions();
- LoggingPreferences logPrefs = new LoggingPreferences();
- logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
- Map<String, Object> perfLogPrefs = new HashMap<String, Object>();
- perfLogPrefs.put("traceCategories", "browser,devtools.timeline,devtools");
- options.setExperimentalOption("perfLoggingPrefs", perfLogPrefs);
// 开启浏览器debugger模式(selenium环境基本配置另行学习) ChromeOptions capabilities = new ChromeOptions(); capabilities.setCapability(ChromeOptions.CAPABILITY, options); capabilities.setCapability(CapabilityType.LOGGING_PREFS, logPrefs); capabilities.setExperimentalOption("debuggerAddress", "localhost:9527");
// 日志自定义提取 LogEntries logEntries = driver.manage().logs().get(LogType.PERFORMANCE); for (LogEntry entry : logEntries) { System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " ======" + entry.getMessage()); System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel().INFO + " INFO======== " + entry.getMessage()); System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel().ALL + " ALL ======== " + entry.getMessage()); System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel().SEVERE + " SEVERE ======== " + entry.getMessage()); System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel().WARNING + " WARNING ======== " + entry.getMessage()); //do something useful with the data }
本操作只是打基本的核心代码写清楚,基础环境配置和原理
参考Selenium