When I use QWebInspector (python, PyQT4), it always opens with default activated "Elements" tab.
Is there a way programmatically switch tab to Network?

Now it looks as: QWebInspector opens "Elements" tab

What I want to see: enter image description here

Script source:

import sys, PyQt4.QtCore, PyQt4.QtGui, PyQt4.QtWebKit

if __name__ == '__main__':
    app = PyQt4.QtGui.QApplication(sys.argv)
    webview = PyQt4.QtWebKit.QWebView()
    inspector = PyQt4.QtWebKit.QWebInspector()

    webview.page().settings().setAttribute(
        PyQt4.QtWebKit.QWebSettings.DeveloperExtrasEnabled, True
    )
    inspector.setPage(webview.page())

    inspector.showMaximized()

    webview.load(PyQt4.QtCore.QUrl('http://yahoo.com'))
    app.exec_()

Surely, appropriate С++/QT method is also suitable! Thanks!

有帮助吗?

解决方案

WebInspector is based on web. You will need to get first and only child of inspector, it will be QWebView.

Like that:

QList<QWidget*> list = inspector.findChildren<QWidget *>();
QWebView* wv =(QWebView*) list.at(0);

Then connect to javaScriptWindowObjectCleared signal of this view, and in the connected slot execute javascript. You will need object to handle this. I called it someObject, for example

QObject::connect(
    wv->page()->mainFrame(), 
    SIGNAL(javaScriptWindowObjectCleared()),
    someObject, 
    SLOT(openNetworkTab())
);

add slot to someObject's class:

void openNetworkTab(){
    wv->page()->mainFrame()->evaluateJavaScript("document.addEventListener('DOMContentLoaded',function(){setTimeout(function(){document.querySelector('.toolbar-item.network').click()},200);});");
}

Delay 200 just to wait for all event bindings initialization before make click

Here list of all inspector tab classes, just in case: .elements, .resources, .network, .scripts, .timeline, .profiles, .audits, .console

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top