How to open Chrome browser console through Selenium?

To open chrome browser console you can use the ChromeOptions class with --auto-open-devtools-for-tabs argument as follows:

  • Test Configuration:

    • Selenium: Selenium Standalone Server v3.14.0
    • ChromeDriver: ChromeDriver 2.46.628402
    • Chrome: Google Chrome 72.0.3626.96
  • Code Block:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    public class A_Chrome_Browser_Console {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("--disable-extensions");
            options.addArguments("--auto-open-devtools-for-tabs");
            WebDriver driver = new ChromeDriver(options);
            driver.get("https://www.google.com/");
            System.out.println(driver.getTitle());
        }
    }
    
  • Console Output:

    Google
    
  • Browser Console Snapshot:

chrome_browser_console

You can find a relevant based discussion in Opening inspect (pressing F12) on Chrome via Selenium

Leave a Comment