Search code examples
c#selenium-webdriverwebdriverremotewebdriver

Exception when calling driver.Manage().Logs.GetLog() during remote session


I am hitting a System.NotImplementedException when calling var entries = driver.Manage().Logs.GetLog(LogType.Browser); from my code.

I'm setting up my remote driver session as follows:

(...)
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.SetLoggingPreference(LogType.Browser, LogLevel.All);
webDriver = new RemoteWebDriver(new Uri(remoteServerUrl),chromeOptions.ToCapabilities());
(...)

From looking deeper into the issue I've found conflicting reports which say that the C# bindings for GetLogs() method in webdriver are not yet implemented - see here. Which would explain the exception I'm getting.

But there are also posts which suggest that this should work, on this site and elsewhere. For example here.

Is it the case that this is something which works when running locally but not for remote webdriver sessions?

Can anyone confirm once and for all the current status of this API in C# before I pull anymore of my hair out? :)

For the record I have tried with both Webdriver 3.01 and 2.53.


Solution

  • I had the same issue, and it seems that the full log set is implemented only for FirefoxDriver (although I'm not sure if this still applies for the Geckodriver version).

    So, in order to have a wrapper that can be used for all browser, I have created the following utility methods:

    //can also be used in the class constructor 
    _logs = driver.Manage().Logs;
    
    public void PrintLogs(string logType)
    {
        try
        {
            var browserLogs = _logs.GetLog(logType);
            if (browserLogs.Count > 0)
            {                           
                foreach (var log in browserLogs)
                {
                    //log the message in a file
                }
            }
        }
        catch
        {
            //There are no log types present
        }
    }
    
    public void PrintAllLogs()
    {
        PrintLogs(LogType.Server);
        PrintLogs(LogType.Browser);
        PrintLogs(LogType.Client);
        PrintLogs(LogType.Driver);
        PrintLogs(LogType.Profiler);
    }