Clarkie235
Clarkie235

Reputation: 153

C# Selenium access browser log

Is there a way of capturing browser logs in c# using selenium.

I am looking to capture any JS errors that appear on a particular page. Preferable on Chrome or Firefox.

I have previously done this in Python but can this be done in C#?

Upvotes: 15

Views: 14982

Answers (2)

Daniel Dutra
Daniel Dutra

Reputation: 286

This library can help you with that: JSErrorCollector.

When you're using it you just have to write one line to get all the errors listed:

List<JavaScriptError> jsErrors = JavaScriptError.ReadErrors(driver);  

Upvotes: 0

Florent B.
Florent B.

Reputation: 42518

To set-up and retrieve the log entries with Selenium / Chrome / C# :

ChromeOptions options = new ChromeOptions();
options.SetLoggingPreference(LogType.Browser, LogLevel.Warning);

var driver = new ChromeDriver(options);

driver.Navigate().GoToUrl("http://stackoverflow.com");

var entries = driver.Manage().Logs.GetLog(LogType.Browser);
foreach (var entry in entries) {
    Console.WriteLine(entry.ToString());
}

And with Firefox:

FirefoxOptions options = new FirefoxOptions();
options.SetLoggingPreference(LogType.Browser, LogLevel.Warning);

var driver = new FirefoxDriver(options);

driver.Navigate().GoToUrl("http://stackoverflow.com");

var entries = driver.Manage().Logs.GetLog(LogType.Browser);
foreach (var entry in entries) {
    Console.WriteLine(entry.ToString());
}

Upvotes: 18

Related Questions