How to open Chrome browser console through Seleniu

2020-02-01 16:23发布

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

1条回答
够拽才男人
2楼-- · 2020-02-01 16:42

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

查看更多
登录 后发表回答