0

请帮助获取使用 Devtools 网络拦截的请求的响应正文(作为 json)。下面是我可以尝试的代码。谢谢!

devTools.addListener(Network.requestWillBeSent(),
                entry -> {
                    Request req;
                    RequestId rid=entry.getRequestId();
                    if (entry.getRequest().getUrl().contains("tender")) {
                        req=entry.getRequest();
                    }
                    try {
                        br.write("Request URI : " + entry.getRequest().getUrl()+"\n With method : "+entry.getRequest().getMethod() + "\n");
                    } catch (IOException e) {e.printStackTrace();}

                    Command <ResponseBody> resBody=Network.getResponseBody(rid);

                });
4

1 回答 1

0

显然,您只是在创建命令对象,而不是执行和检索结果。

尝试这个:

Command<GetResponseBodyResponse> getBody = Network.getResponseBody(responseReceived.getRequestId());
GetResponseBodyResponse response = driver.getDevTools().send(getBody);
ObjectMapper objectMapper = new ObjectMapper();
try {
    JsonNode n = objectMapper.readValue(response.getBody(), JsonNode.class);
    debug("Response from Command: " + n.toPrettyString());
} catch (JsonMappingException e) {
        e.printStackTrace();
} catch (JsonProcessingException e) {
        e.printStackTrace();
}

为了获得这个响应数据,我ResponseReceived使用Network.responseReceived()您的 DevTools-Instance 注册了 -Event。

于 2021-08-10T09:59:51.537 回答