【问题标题】:Is it possible to automatically open Google Chrome devtools?是否可以自动打开 Google Chrome 开发工具?
【发布时间】:2014-02-20 14:22:07
【问题描述】:

鉴于 Selenium 2 关闭了包含我的模拟器配置文件的 devtools 窗口,该配置文件保存在我的 chrome 用户配置文件下。有没有办法使用 selenium 脚本触发 devtools 打开?

这里是关于 devtools 窗口关闭问题的信息 https://sites.google.com/a/chromium.org/chromedriver/help/devtools-window-keeps-closing

尝试其中一些 Chromium 覆盖参数时我感到有些疲惫,只有其中一个似乎有效 http://peter.sh/experiments/chromium-command-line-switches/

有任何影响的是以下

options.addArguments("user-data-dir=/Path/to/chrome/profile");

如果无法打开开发工具窗口或面板,有没有办法初始化模拟器?

【问题讨论】:

    标签: selenium selenium-webdriver google-chrome-devtools junit4 selenium-chromedriver


    【解决方案1】:

    在终端输入以下 -

    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --auto-open-devtools-for-tabs
    

    【讨论】:

    【解决方案2】:

    您也可以使用 Robot 类。它只是帮助您在任何浏览器上打开开发工具。

    在你想打开开发工具的地方使用这段代码

        try{
            Robot robot=new Robot();
            robot.keyPress(KeyEvent.VK_F12);
            robot.keyRelease(KeyEvent.VK_F12);  
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    

    【讨论】:

    • 嗨舒巴姆。知道如何从 DEV 工具的控制台选项卡中捕获文本吗?
    【解决方案3】:

    您可以使用下面给定的代码,它可以在 FireFox 上正常工作,只需更改浏览器

    public void open(String url) {
        driver.get(url);
        Actions action = new Actions(driver);
        String pressF12=Keys.chord(Keys.F12,"");
        driver.findElement(By.id("lst-ib")).sendKeys(pressF12);
    }
    

    【讨论】:

      【解决方案4】:

      正如您提供的同一链接所述,自 2.x 以来,无法将 ChromeDev 工具与 Selenium 的 ChromeDriver 一起使用。这只是直接冲突,您可以使用其中一个或另一个。

      执行解决方法的唯一方法是暂停所有 chrome 驱动程序交互,通过其他一些自动化框架(如 Robots API)启动 DevTool,做任何你需要的事情,然后继续。

      但我可能会考虑我需要从开发工具中获得什么,难道没有替代方案可以通过在启动时为 Chrome 提供适当的配置来满足您的需要吗? (加载不同的配置文件,或加载模拟设备)

      【讨论】:

      【解决方案5】:

      为什么还需要 chrome devtools?

      • 是否用于监控网络流量?为什么不使用带有firebug.xpibrowsermobproxy 的firefoxdriver。
      • 在模拟设备的浏览器中打开页面。这可以通过 chrome 完成,无需打开开发工具。

      带有 chrome 模拟浏览器的 WebDriver

      这是一个代码 sn-p,您可以使用它在特定的模拟版本中启动 chrome 浏览器。注意这里的关键是 deviceName 值,它应该与您的 chrome 浏览器中提到的匹配。

      public static WebDriver MobileOpen()
      {
          Map<String, String> mobileEmulation = new HashMap<String, String>();
          System.setProperty("webdriver.chrome.driver","path/to/chromedriver");
          mobileEmulation.put("deviceName", "Google Nexus 6");
          Map<String, Object> chromeOptions = new HashMap<String, Object>();
          chromeOptions.put("mobileEmulation", mobileEmulation);
      
          DesiredCapabilities capabilities = DesiredCapabilities.chrome();
          capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
      
          WebDriver driver = new ChromeDriver(capabilities);
          return driver;
      }
      

      希望这会有所帮助。
      编辑:我刚看到这是一篇 3 年前的帖子。这家伙可能已经找到回家的路了。 :P

      【讨论】:

        【解决方案6】:

        您是否尝试过console API 来满足您的需求?它将允许您从 JS 内部调用一些开发工具函数。

        【讨论】:

        • 如果您的测试脚本调用击键 [Ctrl+Shift+I] 会完成这项工作吗?
        • Selenium 使浏览器自动化。你的关键用途是打开开发工具,但你不能用它做任何事情,
        猜你喜欢
        • 2014-02-20
        • 2014-01-31
        • 2012-09-09
        • 2016-09-12
        • 1970-01-01
        • 1970-01-01
        • 2011-01-20
        • 2012-08-26
        • 2013-05-15
        相关资源
        最近更新 更多