How to get Chrome browser console log [INFO] entries with Selenium
Asked Answered
J

3

8

I am having issues getting chrome browser console log [INFO] entries with Selenium and the only Level type of entries I am getting are the errors(WARNING, SEVERE).

Is there any way to get anything different than the error entries as I need to get the [INFO] entries and assert based on their content, I have read recently that Selenium is able to return entries only for errors is that accurate?

Would really appreciate any information given, thank you for your attention and time!

Jailbreak answered 6/2, 2019 at 22:9 Comment(1)
Possible duplicate of Selenium Web Driver: Extracted Chrome Browser logs are incompleteEntellus
J
3

thank you for your answer but unfortunately none of them worked for me, I have managed to come up with a solution for my issue using:

    ChromeOptions options = new ChromeOptions();
    options.setCapability(ChromeOptions.CAPABILITY, getCap());
    WebDriver driver = new ChromeDriver(options);

Alongside with the custom made method:

private static DesiredCapabilities getCap() {
    DesiredCapabilities caps = DesiredCapabilities.chrome();
    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
    logPrefs.enable(LogType.PROFILER, Level.INFO);
    logPrefs.enable(LogType.BROWSER, Level.INFO);
    logPrefs.enable(LogType.CLIENT, Level.INFO);
    logPrefs.enable(LogType.DRIVER, Level.INFO);
    logPrefs.enable(LogType.SERVER, Level.INFO);
    caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
    return caps;
}

And finally had to make a filter method as the Selenium one was not working properly for me and was returning all of the entries:

private static List<LogEntry> filterLog(LogEntries entries) {
    List<LogEntry> logs = new ArrayList<>();
    for (LogEntry entry : entries) {
        if(entry.getLevel().toString().equals(INFO)) {
            logs.add(entry);
        }
    }
    return logs;
}
Jailbreak answered 7/2, 2019 at 9:42 Comment(0)
D
2

From the documentation (http://chromedriver.chromium.org/logging):

By default ChromeDriver logs only warnings/errors to stderr. When debugging issues, it is helpful to enable more verbose logging.

Also from documentation:

System.setProperty("webdriver.chrome.logfile", "D:\\chromedriver.log");
System.setProperty("webdriver.chrome.verboseLogging", "true");

Hope that works.

Alternative Option

You could try the following if the above doesn't work, this time using the ChromeOptions object instead:

ChromeOptions options = new ChromeOptions();
options.setArguments("--log-level=1");

or from (https://www.chromium.org/for-testers/enable-logging)

To enable logging, launch Chrome with these command line flags: --enable-logging --v=1

Which translates into:

options.setArguments("--enable-logging --v=1");
Durand answered 7/2, 2019 at 8:25 Comment(2)
It didn't work for me. It is still logging only errors.Mintun
This worked for me. Thanks a lot! This saved my day!Squires
B
0

For anyone doing this using chrome.Options

Simply set the capability:

options = Options()
options.set_capability('goog:loggingPrefs', { 'browser':'ALL' })

To use the logs:

logs = driver.get_log("browser")
for log in logs:
    print(log)
Bales answered 27/11, 2023 at 6:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.