8

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. ?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Pratik Londhe
  • 101
  • 1
  • 1
  • 3
  • You cannot do that with `Actions.sendKeys`. As Javadoc states it sends keys to the active element (meaning an an element of the DOM). Ctrl+Shift+J is a hotkey of the browser application. `org.openqa.selenium.interactions.Keyboard.sendKeys` seems to handle browser keys but I have never used that feature. – Würgspaß Feb 08 '19 at 11:02

1 Answers1

9

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks you, Let me try. – Pratik Londhe Feb 09 '19 at 14:11
  • 5
    It's not console, it's devtools. How did you get any votes on this? – BVengerov Jul 18 '19 at 13:49
  • @BVengerov Let me know if this [screenshot](https://i.stack.imgur.com/nTY7V.png) clears your confusion – undetected Selenium Jul 19 '19 at 06:19
  • 1
    @DebanjanB By default the "Elements" tab is opened, and console is hidden. This you can perfectly see from the screenshot in your answer. If you can click on "console" tab to open the console, nothing prevents you to put a breakpoint in the test and open it with a keystroke - why even bother making changes to the Chrome arguments. BUT if you have only VNC in viewing mode, as I do, then this doesn't help at all. – BVengerov Jul 19 '19 at 06:36
  • @BVengerov Possibly now just for the sake of argument you are deviating from your original counter question. Irrespective of `Elements` tab or `Console` tab being opened there is a definite objective behind opening the _Chrome Browser Console_. If you have a usecase/question feel free to raise a new ticket. StackOverflow contributors will be happy to help you out. – undetected Selenium Jul 19 '19 at 06:49