Controlling Chrome Devtools with Selenium Webdriver

人盡茶涼 提交于 2019-12-04 05:28:50

In Selenium 4 alpha, there is a way to interact with DevTools API using the java-client. What you are looking for specifically is the "Profiler" domain (https://chromedevtools.github.io/devtools-protocol/tot/Profiler)

Recently, I contributed the "Network" and "Performance" domains for a better user facing API in selenium java - https://github.com/SeleniumHQ/selenium/pull/7212

I believe that "Profiler" will also be implemented soon. Of course, there is a generic API for all domains in Java client that was merged a while ago, you can use it like this:

     driver.getDevTools().createSession();

    driver.getDevTools().send(new Command("Profiler.enable", ImmutableMap.of()));
    driver.getDevTools().send(new Command("Profiler.start", ImmutableMap.of()));

    //register to profiler events
    driver.getDevTools().addListener(new Event("Profiler.consoleProfileStarted", ConsoleProfileStarted.class), new Consumer<Object>() {
        @Override
        public void accept(Object o) {
            //do something
        }
    });

Until the Profiler domain will added to Selenium java client, you will have to supply your Mapper.

One thing you can do is the following: - set focus on dev tools window - get url of dev tools window driver.url.toString(); - open a new tab with that url

from there you can inspect the devtools window and are able to interact with the different elements

if you cant access elements that are below the level of a shadow root element it is good to use IJavascriptexecutor with something similar to this: return document.querySelector('element above shadow root').shadowRoot.querySelector('element below shadow root');

So I was able to use javascript to pass the queried element to webdriver and click it. for entering strings into the dev console I used WinAppDriver to get the window into focus and enter the neccessary command in. It works moderately but is better than i expected.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!