如何通过Selenium打开Chrome浏览器控制台? [英] How to open Chrome browser console through Selenium?

本文介绍了如何通过Selenium打开Chrome浏览器控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在Selenium Webdriver中按键盘键Ctrl + Shift + j来打开chrome浏览器控制台. 我可以使用 Robot 类执行此操作,但我希望在没有 Robot 类的情况下执行此操作.我使用了使用sendKeys的Actions类和Keys类.但是我无法打开浏览器控制台.

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.

是Chrome浏览器版本问题还是操作系统?为什么使用Action类和Keys类无法打开浏览器控制台. ?

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

推荐答案

要打开chrome浏览器控制台,您可以将ChromeOptions类与 --auto-open-devtools-for-tabs 参数一起使用,如下所示:

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

  • 测试配置:

  • Test Configuration:

  • 硒:硒独立服务器v3.14.0
  • ChromeDriver: ChromeDriver 2.46.628402
  • Chrome浏览器: Google Chrome浏览器72.0.3626.96
  • Selenium: Selenium Standalone Server v3.14.0
  • ChromeDriver: ChromeDriver 2.46.628402
  • Chrome: Google Chrome 72.0.3626.96

代码块:

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:

    您可以找到相关的的问题在通过Selenium在Chrome上打开检查(按F12)

    这篇关于如何通过Selenium打开Chrome浏览器控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆