如何通过 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:

  • Selenium:Selenium 独立服务器 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)

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

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

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