无法在Selenium C#中获取Chrome性能日志 [英] Unable to get Chrome Performance logs in Selenium C#

查看:800
本文介绍了无法在Selenium C#中获取Chrome性能日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解决方案中使用了以下nuget软件包

I am using following nuget packages in my solution

  1. Selenium.WebDriver-v3.141.0
  2. Selenium.WebDriver.ChromeDriver-v79.0.3945.3600

使用以下代码,我正在创建一个Chrome驱动程序实例

using following code I am creating a Chrome driver instance

ChromeOptions options = new ChromeOptions();

//Get Performance Logs from Network tab
ChromePerformanceLoggingPreferences perfLogPrefs = new ChromePerformanceLoggingPreferences();
options.PerformanceLoggingPreferences = perfLogPrefs;
options.SetLoggingPreference("performance", LogLevel.All);

(或)

ChromePerformanceLoggingPreferences perfLogPrefs = new 
ChromePerformanceLoggingPreferences();
perfLogPrefs.AddTracingCategories(new string[] { "devtools.timeline" });
options.PerformanceLoggingPreferences = perfLogPrefs;
options.SetLoggingPreference("goog:loggingPrefs", LogLevel.All);
options.AddAdditionalCapability(CapabilityType.EnableProfiling, true, true);

并与此结合

options.AddUserProfilePreference("intl.accept_languages", "en-US");
options.AddUserProfilePreference("disable-popup-blocking", "true");
options.AddArgument("test-type");
options.AddArgument("--disable-gpu");
options.AddArgument("no-sandbox");
options.AddArgument("start-maximized");
options.LeaveBrowserRunning = true;

IWebDriver driver = new ChromeDriver(options);

但是在创建Chrome驱动程序实例时,出现以下错误消息

but while creating Chrome driver instance, I am getting following error message

无效的参数:"firstMatch"的条目0无效 来自无效参数:指定了perfLoggingPrefs,但未启用性能日志记录

invalid argument: entry 0 of 'firstMatch' is invalid from invalid argument: perfLoggingPrefs specified, but performance logging was not enabled

请问我需要进行哪些更改,以获取最新版本的Chrome和Selenium驱动程序的性能日志

May I know what changes do I need to make please to get the performance logs with latest version of Chrome and Selenium driver

使用较低版本的Chrome驱动程序(2.35.0)时,我可以使用以下代码检索性能日志

I am able to retrieve Performance Logs using the below code when I was using lower versions of Chrome driver (2.35.0)

var logs = driver.Manage().Logs.GetLog("performance");

for (int i = 0; i < logs.Count; i++)
{
   Console.WriteLine(logs[i].Message);
}

推荐答案

使用Selenium WebDriver(v4.0.0-alpha04)和Selenium.Chrome.WebDriver(v79.0.0),并使用以下代码,我可以检索到性能日志.

With Selenium WebDriver (v4.0.0-alpha04) and Selenium.Chrome.WebDriver (v79.0.0) and using the following code, I am able to retrieve the performance logs.

ChromeOptions options = new ChromeOptions();

//Following Logging preference helps in enabling the performance logs
options.SetLoggingPreference("performance", LogLevel.All);

//Based on your need you can change the following options
options.AddUserProfilePreference("intl.accept_languages", "en-US");
options.AddUserProfilePreference("disable-popup-blocking", "true");
options.AddArgument("test-type");
options.AddArgument("--disable-gpu");
options.AddArgument("no-sandbox");
options.AddArgument("start-maximized");
options.LeaveBrowserRunning = true;

//Creating Chrome driver instance
IWebDriver driver = new ChromeDriver(options);

//Extracting the performance logs
var logs = driver.Manage().Logs.GetLog("performance");
for (int i = 0; i < logs.Count; i++)
{
   Console.WriteLine(logs[i].Message);
}

希望这会有所帮助.

这些天来,我一直在将以下两行与Selenium WebDriver和Chrome驱动程序的最新版本一起使用,无法弄清问题所在,而对于最新版本,则不需要以下两行代码.

All these days, I was using the following two lines with latest versions of Selenium WebDriver and Chrome Driver and couldn't figure out what the issue was and now with the latest versions, the following two lines of code is not required.

var perfLogPrefs = new ChromePerformanceLoggingPreferences();
options.PerformanceLoggingPreferences = perfLogPrefs;

这篇关于无法在Selenium C#中获取Chrome性能日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆