Driver Log: A Powerful Tool for Debug in Selenium

Table Of Contents

Overview

When working with Selenium, sometimes you may want to get the driver log to know what’s going on. This is really helpful in some situations, especially if it is related to debugging. Below is some methods to get driver log (chrome driver) with Selenium Java.

How to

Firstly, create a new Maven project with your favourite Java IDE (recommend Intellij).

Create a Test class in src/test/java folder.

And you are ready to get chrome driver using one of these below methods:

1. Methods A:

import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;

public class Test {

    public static void main(String[] args) {
        String projectDir = System.getProperty("user.dir");
        System.setProperty("webdriver.chrome.driver", projectDir + File.separator + "chromedriver.exe");
        System.setProperty("webdriver.chrome.logfile", projectDir + File.separator + "chromedriver.log");
        System.setProperty("webdriver.chrome.verboseLogging", "true");

        ChromeDriver driver = new ChromeDriver();
        driver.get("https://ericsson.com");
        driver.manage().window().maximize();

        driver.quit();
    }
}

=> This will generate “chromedriver.log” file in the root of your project. Simple, isn’t it?

2. Methods B:

First, download appropriate chrome driver for your chrome browser (version matching) here

Extract it and copy chromedriver.exe to the root of your project. Next, cd to your project folder and run below command with terminal/command prompt (cmd):

chromedriver.exe --verbose --log-path=chromedriver.log 

At the time you run this command, it will instanly generate a “chromedriver.log”. The remaining task is just to initialize your web driver with RemoteWebDriver and point at “localhost:9515”. Note that 9515 is the default port for chrome driver.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.File;
import java.net.URL;

public class Test {

    public static void main(String[] args) {
        String projectDir = System.getProperty("user.dir");
        System.setProperty("webdriver.chrome.driver", projectDir + File.separator + "chromedriver.exe");

        WebDriver driver = null;
        try {
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.setAcceptInsecureCerts(true);

            driver = new RemoteWebDriver(new URL("http://localhost:9515"), chromeOptions);
            driver.get("https://ericsson.com");
            driver.manage().window().maximize();

            // sleep for a while
            Thread.sleep(3000);

            driver.quit();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Conclusion

Driver log is necessary whenever you want to know what is going on in your Selenium scripts. It is really helpful though. Know how to get and use it as a powerful tool for debugging.

Ask me anything