使用chromedriver打印日志
打印日志,并获取响应状态码
package com.wzq.gbif;
import java.util.Iterator;
import java.util.logging.Level;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Test03 {
public static void main(String[] args) {
downloadPage("https://www.baidu.com/");
}
private static void downloadPage(String url) {
ChromeDriver driver = null;
try {
ChromeOptions options = new ChromeOptions();
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
options.setCapability("goog:loggingPrefs", logPrefs);
driver = new ChromeDriver(options);
System.out.println("Navigate to " + url);
driver.navigate().to(url);
String currentURL = driver.getCurrentUrl();
LogEntries logs = driver.manage().logs().get("performance");
int status = -1;
System.out.println("\\nList of log entries:\\n");
for (Iterator<LogEntry> it = logs.iterator(); it.hasNext(); ) {
LogEntry entry = it.next();
try {
JSONObject json = new JSONObject(entry.getMessage());
System.out.println(json.toString());
JSONObject message = json.getJSONObject("message");
String method = message.getString("method");
if (method != null && "Network.responseReceived".equals(method)) {
JSONObject params = message.getJSONObject("params");
JSONObject response = params.getJSONObject("response");
String messageUrl = response.getString("url");
if (currentURL.equals(messageUrl)) {
status = response.getInt("status");
System.out.println(messageUrl + ": " + status);
System.out.println("headers: " + response.get("headers"));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
System.out.println("\nstatus code: " + status);
} finally {
if (driver != null) {
driver.quit();
}
}
}
}
打印日志信息
Navigate to https://www.baidu.com/
\nList of log entries:\n
{"webview":"4093F206401AE297E4EFF7BEE9AC3FD7","message":{"method":"Network.requestWillBeSent","params":{"request":{"headers":
https://www.baidu.com/: 200
headers: {"Transfer-Encoding":"chunked","Server":"BWS\/1.1","X-Ua-Compatible":"IE=Edge,chrome=1","Connection":"keep-alive","P3p":"CP=\" OTI DSP COR IVA OUR IND COM \"\nCP=\" OTI DSP COR IVA OUR IND COM \"","Date":"Fri, 13 Aug 2021 08:59:41 GMT","X-Frame-Options":"sameorigin","Strict-Transport-Security":"max-age=172800","Cache-Control":"private","Bdpagetype":"1","Content-Encoding":"gzip","Traceid":"162884518124046397549622976489859333826","Set-Cookie":"BAIDUID=F1C5FC92CC8A0F4C4BCC90CD65D5F92D:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=\/; domain=.baidu.com\nBIDUPSID=F1C5FC92CC8A0F4C4BCC90CD65D5F92D; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=\/; domain=.baidu.com\nPSTM=1628845181; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=\/; domain=.baidu.com\nBAIDUID=F1C5FC92CC8A0F4C11A03438D50332A3:FG=1; max-age=31536000; expires=Sat, 13-Aug-22 08:59:41 GMT; domain=.baidu.com; path=\/; version=1; comment=bd\nBDSVRTM=0; path=\/\nBD_HOME=1; path=\/\nH_PS_PSSID=34334_34369_31660_34404_34004_34092_34106_34111_26350_34422_22158_34390; path=\/; domain=.baidu.com","Bdqid":"0x858bae2200003ec2","Expires":"Fri, 13 Aug 2021 08:59:20 GMT","Content-Type":"text\/html;charset=utf-8"}
…………
status code: 200