use*_*411 5 java selenium google-chrome google-chrome-devtools selenium-chromedriver
我最近一直在使用 selenium webdriver 和 chrome 以及日志。但是任何时间戳值都以奇怪的日期时间戳格式返回。我找遍了,我不知道它是什么。此外,时间戳以外的其他值(如 requestId 或 walltime)也采用新的未知格式。这是什么格式,我怎样才能把它变成正常的 (MM DD YYYY HH:MM:SS..) 格式?
2021 年 6 月 23 日左右的时间戳为 2484894.662632,2021 年 6 月 23 日左右的 10:53:23.118 时间戳为 2486019.900761,2021 年 6 月 23 日左右,11:12:01.277 时间戳为 2581859.5 2021 年 6 月 23 日左右的 2581859.59:204:14:25
例子:
"requestId":"30432.634","timestamp":87693.142713,"type":"XHR","wallTime":1624556888.229531}
Run Code Online (Sandbox Code Playgroud)
代码片段:
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
flavorCapability.setCapability("goog:loggingPrefs", logPrefs);
driver.manage().logs().get(LogType.PERFORMANCE).getAll();
Run Code Online (Sandbox Code Playgroud)
有两种方法可以得到想要的结果:
1)简单的方法:
LogEntries entries = driver.manage().logs().get(LogType.PERFORMANCE);
for(LogEntry entry: entries){
System.out.println(entry.getTimestamp());
System.out.println(entry.getLevel());
System.out.println(entry.getMessage());
System.out.println(entry.toJson());
System.out.println(new Date(entry.getTimestamp()));
}
Run Code Online (Sandbox Code Playgroud)
2)第二种方法:
import org.json.JSONException;
import org.json.JSONObject;
LogEntries logs = driver.manage().logs().get("performance");
for (Iterator<LogEntry> it = logs.iterator(); it.hasNext();) {
LogEntry entry = it.next();
try {
JSONObject json = new JSONObject(entry.getMessage());
JSONObject message = json.getJSONObject("message");
String method = message.getString("method");
System.out.println(method);
if (method != null && "Network.responseReceived".equals(method)) {
JSONObject params = message.getJSONObject("params");
JSONObject response = params.getJSONObject("response");
JSONObject headers = response.getJSONObject("headers");
String timestamp = headers.getString("date");
String url = response.getString("url");
int status = response.getInt("status");
System.out.println("Response = " + response);
System.out.println("URL = "+ url);
System.out.println("Status Code = "+ status);
System.out.println("headers: " + response.get("headers"));
System.out.println("Timestamp: " + timestamp);
}
} catch (JSONException e) {
System.out.println(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
参考: https: //chromedevtools.github.io/devtools-protocol/tot/Network/

注:请提供具体的要求,您到底想要得到什么?
| 归档时间: |
|
| 查看次数: |
165 次 |
| 最近记录: |