Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I want to build an automation testing, so I have to know the errors that appear in the console of chrome.

there is an option to get the error lines that appear in the console?

In order to see the console: right click somewhere in the page, click "inspect element" and then go to "console".

1 Answer

0 votes
by (62.9k points)

Here's Java code that does the job, I hope you can translate it to C#

import org.openqa.selenium.WebDriver;


import org.openqa.selenium.logging.LogEntries;

import org.openqa.selenium.logging.LogEntry;

import org.openqa.selenium.logging.LogType;

import org.openqa.selenium.logging.LoggingPreferences;

import org.openqa.selenium.remote.CapabilityType;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

public class ChromeConsoleLogging 

private WebDriver driver;


public void setUp() {



public void tearDown() 

public void analyzeLog() 

LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);

for (LogEntry entry : logEntries) {

System.out.println(new Date(entry.getTimestamp()) + 



public void testMethod() 



Pay attention to the setUp method in the above code. We use the LoggingPreferences object to enable logging. There are a few kinds of logs, but if you want to track console errors then LogType.BROWSER is the one that you must use. Then we pass that object to DesiredCapabilities and further to ChromeDriver constructor, then we will have an instance of ChromeDriver with logging enabled.

After performing some actions on the page we call the analyzeLog() method.

Here we tend to merely extract the log and reiterate through its entries.

Here you'll place assertions or do the other reporting you would like.

Browse Categories