C # Selenium access browser log

Is there a way to capture browser logs in C # using selenium.

I am looking to capture any JS errors that appear on a specific page. Recommended in Chrome or Firefox.

I already did this in Python, but can this be done in C #?

+5
source share
2 answers

To configure and get log entries using 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()); } 
+3
source

This library can help you with this: JSErrorCollector .

When you use it, you just need to write one line to get all the errors listed:

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

Source: https://habr.com/ru/post/1246568/


All Articles