我正在研究硒,以跟踪网站的网络流量。
我遵循了两个链接:
1. Using Selenium how to get network request
2. http://chromedriver.chromium.org/logging/performance-log
配置后,我得到了日志数据。
您可以参考第2点获取示例数据:
这就是我得到的
[2015-03-21T16:50:20 + 0400] [INFO] {“ message”:{“ method”:“ Network.responseReceived”,“ params”:{“ frameId”:“ 28480.1”,“ loaderId”:“ 28480.2“,” requestId“:” 28480.1“,” response“:{” connectionId“:0,” connectionReused“:false,” encodedDataLength“:-1,” fromDiskCache“:false,” fromServiceWorker“:false,”标头“ :{“ Access-Control-Allow-Origin”:“ *”,“ Content-Type”:“文本/纯文本; charset = US-ASCII”},“ mimeType”:“文本/纯文本”,“协议”:“数据”,“状态”:200,“ statusText”:“确定”,“ URL”:“数据:”,“},“时间戳”:1426942217.5344,“类型”:“其他”}},“网络视图”:“ C359224A -06E5-42B6-8D1B-52687733920A“}
[2015-03-21T16:50:20 + 0400] [INFO] {“ message”:{“ method”:“ Network.loadingFinished”,“ params”:{“ encodedDataLength”:0,“ requestId”:“ 28480.1” ,“时间戳”:1426942217.5344}},“网页视图”:“ C359224A-06E5-42B6-8D1B-52687733920A”}
[2015-03-21T16:50:20 + 0400] [INFO] {“ message”:{“ method”:“ Page.frameNavigated”,“ params”:{“ frame”:{“ id”:“ 28480.1”, “ loaderId”:“ 28480.2”,“ mimeType”:“ text / plain”,“ securityOrigin”:“://”,“ url”:“ data :,”}}},“ webview”:“ C359224A-06E5- 42B6-8D1B-52687733920A“}
[2015-03-21T16:50:21 + 0400] [INFO] {“ message”:{“ method”:“ Page.loadEventFired”,“ params”:{“ timestamp”:1426942220.99924}},“ webview”:“ C359224A-06E5-42B6-8D1B-52687733920A“}
[2015-03-21T16:50:21 + 0400] [INFO] {“ message”:{“ method”:“ Page.frameStoppedLoading”,“ params”:{“ frameId”:“ 28480.1”}},“ webview” :“ C359224A-06E5-42B6-8D1B-52687733920A”}
[2015-03-21T16:50:21 + 0400] [INFO] {“ message”:{“ method”:“ Page.domContentEventFired”,“ params”:{“ timestamp”:1426942220.99927}},“ webview”:“ C359224A-06E5-42B6-8D1B-52687733920A“}
....
但是我不知道如何计算一个请求xhr请求的时间?
我已经搜索过了,但是现在运气...
最佳答案
启用PERFORMANCE日志后,可以使用以下命令迭代日志条目:
List<LogEntry> entries = this.driver.manage().logs().get(LogType.PERFORMANCE).getAll();
然后,您可以使用以下值获取json:
private String url;
private Boolean timing;
private Long connectEnd;
private Long connectStart;
private Long dnsEnd;
private Long dnsStart;
private Long receiveHeadersEnd;
private Long sslStart;
private Long sslEnd;
private Long proxyStart;
private Long proxyEnd;
private Long sendStart;
private Long sendEnd;
private String mimeType;
private String requestStatus;
private String method;
要获取单个请求的总时间,您可以使用:
sendStart-sendEnd
Chrome驱动程序的创建:
DesiredCapabilities capabilities = new DesiredCapabilities();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
logPrefs.enable(LogType.DRIVER, Level.ALL);
capabilities.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
Map<String, Object> perfLogPrefs = new HashMap<String, Object>();
perfLogPrefs.put("traceCategories", "browser,devtools.timeline,devtools");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("perfLoggingPrefs", perfLogPrefs);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);