Capturing JavaScript error in Selenium.

We can capture JavaScript errors in Selenium WebDriver to identify console errors that appear in the browser's Developer Tools. These errors can occur due to functional issues on the page or performance problems caused by excessive logging.

JavaScript errors are typically visible in the Console tab of browser Developer Tools and can impact application functionality. Selenium provides logging capabilities to capture and analyze these errors programmatically.

Browser Developer Tools - Console Tab ? TypeError: Cannot read property 'value' of null at script.js:15:23 ?? Warning: Performance issue detected at app.js:42:10 ?? Info: API call completed at network.js:8:5 Selenium WebDriver captures these logs

Setting Up Log Capture

We can capture JavaScript errors using Selenium's logging API through the WebDriver's manage() method. This allows us to retrieve browser console logs and filter them by severity level.

import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import java.util.logging.Level;

Complete Example

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 java.util.logging.Level;
import java.util.Set;

public class JavaScriptErrorCapture {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", 
            "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
        
        WebDriver driver = new ChromeDriver();
        String url = "https://the-internet.herokuapp.com/javascript_error";
        driver.get(url);
        
        // Maximize browser window
        driver.manage().window().maximize();
        
        // Get available log types
        Set<String> logTypes = driver.manage().logs().getAvailableLogTypes();
        System.out.println("Available log types: " + logTypes);
        
        // Capture browser console logs
        LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
        
        // Filter and display all log entries
        for (LogEntry logEntry : logEntries) {
            System.out.println("Level: " + logEntry.getLevel());
            System.out.println("Message: " + logEntry.getMessage());
            System.out.println("Timestamp: " + logEntry.getTimestamp());
            System.out.println("---");
        }
        
        // Filter only severe errors
        System.out.println("Severe errors only:");
        for (LogEntry logEntry : logEntries) {
            if (logEntry.getLevel().equals(Level.SEVERE)) {
                System.out.println("SEVERE ERROR: " + logEntry.getMessage());
            }
        }
        
        driver.quit();
    }
}

Key Methods

Method Purpose Returns
driver.manage().logs().getAvailableLogTypes() Get all available log types Set<String>
driver.manage().logs().get(LogType.BROWSER) Get browser console logs LogEntries
logEntry.getLevel() Get log severity level Level (INFO, WARNING, SEVERE)
logEntry.getMessage() Get error message content String

Log Levels

JavaScript errors are categorized by severity levels:

  • SEVERE: Critical errors that break functionality
  • WARNING: Potential issues that may affect performance
  • INFO: Informational messages and general logs
  • ALL: Captures all log levels

Common Use Cases

Capturing JavaScript errors is useful for:

  • Identifying console errors during automated testing
  • Monitoring application performance issues
  • Debugging failing test scenarios
  • Validating error-free page loads

Conclusion

Selenium's logging API provides powerful capabilities to capture JavaScript errors from browser console logs. Use LogType.BROWSER with appropriate level filtering to identify and debug application issues during automated testing.

Updated on: 2026-03-15T23:19:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements