Selenium 4 Features – Retrieving Browser Console Log Using Selenium WebDriver
Selenium 4 is out ( Alpha version at the time of writing this post). They have given a very important feature in Selenium 4 which is Logging. Logging is generally helpful to know what went wrong.
When you launch a website in browser, you can see logs in console tab of Developer tool. These logs are important to find bugs which may be missed by manual testers or automation tests. Red color logs are Errors while yellow colored logs are Warnings.
![](http://makeseleniumeasy.com/wp-content/uploads/2019/12/image-4.png)
Now it is possible to capture these logs using Selenium and we can analyse these logs to find missed bugs. Developers will also get a better idea from these logs. This log type is called Browser level.
Selenium WebDriver is providing five log types as of now:-
- Browser:- Javascript console logs from the browser
- Client :- Logs from the client side implementation of the WebDriver protocol (e.g. the Java bindings)
- Driver:- Logs from the internals of the driver (e.g. FirefoxDriver internals)
- Performance :- Logs relating to the performance characteristics of the page under test (e.g. resource load timings)
- Server :- Logs from within the selenium server.
In this example we are focusing on Browser level Log type. Below are some important points:-
- LogType is a class which provides you static final constants such as to pass log type.
- LogEntries is a class which is iterable which consists log details.
- Option inner interface of WebDriver interface consists “logs()” methods. We can access it using driver.manage().logs()
Program:-
package Selenium4Features; import java.awt.AWTException; import java.util.List; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.logging.LogEntries; import org.openqa.selenium.logging.LogEntry; import org.openqa.selenium.logging.LogType; import io.github.bonigarcia.wdm.WebDriverManager; public class ConsoleLog { public static void main(String[] args) throws AWTException { // Setting chrome executable WebDriverManager.chromedriver().setup(); // Launching browser WebDriver driver = new ChromeDriver(); // Loading URL driver.get("http://makeseleniumeasy.com/"); // Mentioning type of Log LogEntries entry = driver.manage().logs().get(LogType.BROWSER); // Retrieving all log Listlogs= entry.getAll(); // Print one by one for(LogEntry e: logs) { System.out.println(e); } // Printing details separately for(LogEntry e: logs) { System.out.println("Message is: " +e.getMessage()); System.out.println("Level is: " +e.getLevel()); } } }
Output:-
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Starting ChromeDriver 78.0.3904.105 (60e2d8774a8151efa6a00b1f358371b1e0e07ee2-refs/branch-heads/3904@{#877}) on port 22084 Only local connections are allowed. Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code. [1576137115.752][WARNING]: Timed out connecting to Chrome, retrying... Dec 12, 2019 1:21:57 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C [2019-12-12T13:21:59+0530] [SEVERE] http://makeseleniumeasy.com/wp-content/plugins/email-subscribe/js/subscribe-popup.js?ver=5.1.3 1101:20 Uncaught TypeError: Cannot read property 'css' of undefined [2019-12-12T13:21:59+0530] [SEVERE] http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js 42:397 Uncaught N: adsbygoogle.push() error: Fluid responsive ads must be at least 250px wide: availableWidth=220 Message is: http://makeseleniumeasy.com/wp-content/plugins/email-subscribe/js/subscribe-popup.js?ver=5.1.3 1101:20 Uncaught TypeError: Cannot read property 'css' of undefined Level is: SEVERE Message is: http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js 42:397 Uncaught N: adsbygoogle.push() error: Fluid responsive ads must be at least 250px wide: availableWidth=220 Level is: SEVERE
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.
how do we extract a particular text from the logs?could u please help on this ?
Hi, the above thing doesn’t works if I use RemoteDriver, I need to execute my scripts in remote machine and is there any way to get console errors
Hi,
Thanks for the above code, but we are not using chromium driver
Was very helpful. How does this differ from Selenium version 3 or even version 2 though?
Very helpfull Amodh..Thanks a lot..