0

Is there a way to get all the console log and events fired when navigating on a website?

I have tried using the answers on Capturing browser logs with Selenium WebDriver using Java but analyzeLog() does not return anything when I use it

 System.setProperty("webdriver.chrome.driver", "C:\\Automation//chromedriver.exe");
 ChromeOptions options = new ChromeOptions();
 options.setCapability(ChromeOptions.CAPABILITY, options);
 LoggingPreferences logPrefs = new LoggingPreferences();
 logPrefs.enable(LogType.BROWSER, Level.ALL);
 options.setCapability("goog:loggingPref", logPrefs);
 options.addArguments("incognito");

 driver = new ChromeDriver(options);
 driver.manage().window().maximize();
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}



public void log() {
 LogEntries logEntries = driver.manage().logs().get(LogType.CLIENT);
 for (LogEntry entry: logEntries) {
  System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
 }
 System.out.println("=======================================================");
}
Khalish
  • 1
  • 1
  • pretty sure that was supposed to be with an 's' on the end `options.setCapability("goog:loggingPref", logPrefs);` should have been ? `options.setCapability("goog:loggingPrefs", logPrefs);` –  Nov 24 '20 at 12:11

2 Answers2

0

You can use .setLogLevel, but this method available only with RemoteWebDriver initialize, and seem like your driver initialized by WebDriver.

Add cast to your driver.

driver = new ChromeDriver(options);
((RemoteWebDriver) driver).setLogLevel(Level.INFO);
driver.manage().window().maximize();

Following import:

import java.util.logging.Level;
import org.openqa.selenium.remote.RemoteWebDriver;
frianH
  • 7,295
  • 6
  • 20
  • 45
0

The problem is with your code:

Step 1: Enable PERFORMANCE and BROWSER logtype

LoggingPreferences logPrefs = new LoggingPreferences();
//To get network log
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
//To get console log
logPrefs.enable(LogType.BROWSER, Level.ALL);

Step 2:

You are using the incorrect setting.

Incorrect: options.setCapability("goog:loggingPref", logPrefs);

Correct: options.setCapability( "goog:loggingPrefs", logPrefs );

Step 3:

Correct the code as per the requirement:

//TO get network logs
 LogEntries logEntries = driver.manage().logs().get(LogType.PERFORMANCE);
//TO get console logs
 LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
Abhishek Dhoundiyal
  • 1,359
  • 1
  • 12
  • 19