javascript - 如何通过json提取Chrome开发者工具的网络选项卡内容

标签 javascript json selenium-webdriver google-chrome-devtools

我正在尝试使用 json 通过 selenium webdriver 提取 chrome 开发人员工具的网络调用,是否有其他 json 格式可用于提取网络大小或内容长度?

最佳答案

您可以使用 LoggingPreferences 来获取性能日志。它以 json 格式返回数据。这是一个示例 java 代码。在 Ubuntu 14.04 上使用 selenium 2.53、chromedriver 2.20、Chrome 50 进行了测试。这应该也适用于 Windows。

    DesiredCapabilities d = DesiredCapabilities.chrome();
    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
    d.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
    WebDriver driver = new ChromeDriver(d);
    driver.get("http://www.google.com");
    LogEntries les = driver.manage().logs().get(LogType.PERFORMANCE);
    for (LogEntry le : les) {
        System.out.println(le.getMessage());
    }

这是一个示例输出。它是手动格式化的。实际输出在一行中。

{
    "message": {
        "method": "Network.requestWillBeSent",
        "params": {
            "documentURL": "https://www.google.co.in/?gfe_rd=cr&ei=gpwxV4OSKMmR2ASEg6-YCg&gws_rd=ssl",
            "frameId": "31172.2",
            "initiator": {
                "stack": {
                    "callFrames": [
                        {
                            "columnNumber": 11511,
                            "functionName": "",
                            "lineNumber": 55,
                            "scriptId": "50",
                            "url": "https://www.google.co.in/?gfe_rd=cr&ei=gpwxV4OSKMmR2ASEg6-YCg&gws_rd=ssl"
                        }
                    ]
                },
                "type": "script"
            },
            "loaderId": "31172.3",
            "request": {
                "headers": {
                    "Accept": "*/*",
                    "Referer": "https://www.google.co.in/",
                    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36"
                },
                "initialPriority": "Low",
                "method": "GET",
                "mixedContentType": "none",
                "url": "https://www.google.co.in/xjs/_/js/k=xjs.s.en.VTDhrkH4c9U.O/m=sx,c,sb,cdos,cr,elog,jsa,r,hsm,qsm,j,p,d,csi/am=AJQ0CwoS8fchIGwhrCA1YGBR/rt=j/d=1/t=zcms/rs=ACT90oGi2YIjVL5cBzOc1-MD37a1NqZ1jA"
            },
            "requestId": "31172.3",
            "timestamp": 251208.074288,
            "type": "Other",
            "wallTime": 1462869123.92204
        }
    },
    "webview": "8AF4A466-8027-4340-B9E9-CFEBDA769C50"
}

关于javascript - 如何通过json提取Chrome开发者工具的网络选项卡内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32219113/

相关文章:

javascript - If else 重构包含逻辑

javascript - 正则表达式在双引号文本中查找超过 2 个花括号

javascript - Sequelize.js 一对多关系外键

json - spark 如何从 JSON 推断数值类型?

python - Chrome 已关闭,错误为 ERROR :network_change_notifier_win. cc(141)] WSALookupServiceBegin 失败,原因为:0

javascript - 如何在 Protractor 中单击没有文本的按钮?

javascript - 清除数组后不会更新 ng-options 绑定(bind)变量

javascript - Highchart 未使用外部 json 数据填充

javascript - 如何使用 jQuery ajax 通过在 JSON 文件中传递 id 来仅引入过滤后的数据?

java - 为 selenium webdriver 数据驱动框架读取 excel 中的空单元格