小编典典

C#selenium访问浏览器日志

selenium

有没有一种方法可以使用selenium捕获c#中的浏览器日志。

我正在寻找捕获出现在特定页面上的所有JS错误。在Chrome或Firefox上更可取。

我以前在Python中完成过此操作,但是可以在C#中完成吗?


阅读 539

收藏
2020-06-26

共1个答案

小编典典

要使用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());
}

对于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());
}
2020-06-26