Capturing JavaScript error in Selenium.

We can capture Javascript error in Selenium. This type of error appears at the Console Tab on opening the Developer tools in the browser. This can occur due to some functional issue in the page or due to extra logs which may cause performance issues.

We can handle the Javascript errors with the driver object and manage method.


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.List;
import java.util.ArrayList;
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 JavascrptLogErs{
   public static void main(String[] args) {
      WebDriver driver = new ChromeDriver();
      String u = "https://the−";
      // maximize browser
      // to obtain browser errors
      Set<String> logtyp = driver.manage().logs().getAvailableLogTypes();
      for (String s : logtyp) {
      LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
      List<LogEntry> lg = logEntries.filter(Level.ALL);
      for(LogEntry logEntry : lg) {

Updated on: 30-Nov-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started