How to get Chrome Dev Tools logs with Selenium C#?

Viewed 45

My aim is getting the transfer size of page. Explained below image.

enter image description here

How can I get it with C# code using selenium NuGet package? I searched chrome dev tools. https://chromedevtools.github.io/devtools-protocol/tot/Network/

I see there is Network.dataReceived property. As I understand I need to get this.

My code

using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;

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);

var url = " https://...............";
driver.Navigate().GoToUrl(url);

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

I found some code that possibly worked in old versions. I am using Chrome 105 version(as a nuget package, and selenium web dirver 4.0.0) and Chrome should be 105 version. In C# I can not find the below code, this is Java version:

for (LogEntry entry : driver.manage().logs().get(LogType.PERFORMANCE)) {
if(entry.getMessage().contains("Network.dataReceived")) {
    Matcher dataLengthMatcher = Pattern.compile("encodedDataLength\":(.*?),").matcher(entry.getMessage());
    dataLengthMatcher.find();
}

Even if we can do this with C# code, I dont believe driver.Manage().Logs.GetLog("performance") returns the same with the listed lines in dev tools network section. Is there nay option about that?

0 Answers
Related