Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Chrome browser console through Selenium?

I want to open chrome browser console by pressing keyboard keys Ctrl+Shift+j in selenium webdriver. I am able to do this action using Robot class but I want this without Robot class. I have used the Actions class and Keys class using sendKeys. But I am unable to open browser console.

Is it chrome browser version issue or OS? Why the browser console is not opening using Action class and Keys class. ?

like image 585
Pratik Londhe Avatar asked Feb 08 '19 09:02

Pratik Londhe


People also ask

How to invoke Chrome browser in Selenium WebDriver?

For invoking chrome browser, we have to select the Chrome class. Then create the driver object of that class. This is the most important and mandatory step for browser invocation. Every chrome browser gives an executable file. Through Selenium we need to invoke this executable file which is responsible for invoking the actual chrome browser.

How to open console in Chrome browser?

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

How to start and stop a selenium session with a browser?

Once you have a Selenium library installed , and your desired browser driver , you can start and stop a session with a browser. Typically, browsers are started with specific options that describe which capabilities the browser must support, and how the browser should behave during the session.

How to launch a chromedriver application from a specific browser?

The path of the chromedriver.exe file needs to be added in the executable file. Then we need to use the get () method to launch our application in that particular browser.


1 Answers

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 python based discussion in Opening inspect (pressing F12) on Chrome via Selenium

like image 186
undetected Selenium Avatar answered Sep 28 '22 05:09

undetected Selenium