如何使用Selenium中的Chrome DevTools协议(使用Python)来捕获HTTP请求和响应? [英] How to use Chrome DevTools protocol in Selenium (using Python) for capturing HTTP requests and responses?

查看:1552
本文介绍了如何使用Selenium中的Chrome DevTools协议(使用Python)来捕获HTTP请求和响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Fetch Domain用于此目的,但我不知道如何实现它.在Selenium python中,我使用以下代码来启用requestPaused事件的发布.

I know that Fetch Domain is used for this purpose but I do not know how exactly I can implement it. In Selenium python, I used the following code to enable issuing of requestPaused events.

driver.execute_cdp_cmd("Fetch.enable",{})
driver.get('https://www.example.com')

但是我不知道如何处理requestPaused事件(我需要调用一个fulfillRequestcontinueRequest/continueWithAuth).结果,我的程序停止工作. 如果有人可以提供示例帮助我理解它的工作原理,我将非常感激.

But I do not know how can I handle requestPaused event (I need to call one fulfillRequest or continueRequest/continueWithAuth). As a result, my program stops working. I really appreciate it if anyone could provide me an example to help me understand how it works.

推荐答案

,您认为正确.

根据 Selenium v​​4.0.0-alpha-3 的发行说明:

* Expose devtools APIs from chromium derived drivers.
* Expose presence of devtools support on a role-based interface

根据 Selenium v​​4.0.0.0-alpha-1 的发行说明:

* Basic support for CDP landed via the "DevTools" interface.

因此全部设置为可与,它将允许使用工具来检测,检查,调试和分析Chromium,Chrome和其他基于Blink的浏览器.在讨论中,使用Selenium Webdriver控制Chrome Devtools @AdiOhana提到了示例用法来自 Profiler域的一些命令,如下所示:

So chrome-devtools-protocol is all set to be available with selenium4 which will allow for tools to instrument, inspect, debug and profile Chromium, Chrome and other Blink-based browsers. In the discussion Controlling Chrome Devtools with Selenium Webdriver @AdiOhana mentions of the example usage of a few commands from the Profiler Domain as follows:

    driver.getDevTools().createSession();
    driver.getDevTools().send(new Command("Profiler.enable", ImmutableMap.of()));
    driver.getDevTools().send(new Command("Profiler.start", ImmutableMap.of()));
    //register to profiler events
    driver.getDevTools().addListener(new Event("Profiler.consoleProfileStarted", ConsoleProfileStarted.class), new Consumer<Object>() {
        @Override
        public void accept(Object o) {
            //do something
        }
    });

注意:在将Profiler域添加到Selenium Java客户端之前,您将必须提供Mapper.

Note: Until the Profiler domain will added to Selenium java client, you will have to supply your Mapper.


获取域

获取域将使客户端能够用客户端代码代替浏览器的网络层.


Fetch Domain

Fetch Domain will enable clients substitute browser's network layer with client code.

  • 提取域"方法如下:

  • The Fetch Domain methods are as follows:

  • Fetch.disable:禁用获取域.
  • Fetch.enable:启用发出requestPaused事件.一个请求将被暂停,直到客户端调用failRequest,fulfillRequest或continueRequest/continueWithAuth中的一个.
  • Fetch.failRequest:由于特定原因导致请求失败.
  • Fetch.fulfillRequest:提供对请求的响应.
  • Fetch.continueRequest:继续请求,可以选择修改其一些参数.
  • Fetch.continueWithAuth:在authRequired事件之后继续提供authChallengeResponse的请求.
  • Fetch.getResponseBody:使响应的正文从服务器接收并作为单个字符串返回.可能仅针对在Response阶段中暂停并且与takeResponseBodyForInterceptionAsStream互斥的请求发出.在收到正文之前调用影响请求的其他方法或禁用获取域会导致行为未定义.
  • Fetch.takeResponseBodyAsStream:返回代表响应主体的流的句柄.该请求必须在HeadersReceived阶段中暂停.请注意,执行此命令后,请求无法按原样继续-客户端需要取消请求或提供响应正文.该流仅支持顺序读取,如果指定了位置,IO.read将失败.此方法与getResponseBody互斥.在收到正文之前调用影响请求的其他方法或禁用获取域会导致行为未定义.
  • Fetch.disable: Disables the fetch domain.
  • Fetch.enable: Enables issuing of requestPaused events. A request will be paused until client calls one of failRequest, fulfillRequest or continueRequest/continueWithAuth.
  • Fetch.failRequest: Causes the request to fail with specified reason.
  • Fetch.fulfillRequest: Provides response to the request.
  • Fetch.continueRequest: Continues the request, optionally modifying some of its parameters.
  • Fetch.continueWithAuth: Continues a request supplying authChallengeResponse following authRequired event.
  • Fetch.getResponseBody: Causes the body of the response to be received from the server and returned as a single string. May only be issued for a request that is paused in the Response stage and is mutually exclusive with takeResponseBodyForInterceptionAsStream. Calling other methods that affect the request or disabling fetch domain before body is received results in an undefined behavior.
  • Fetch.takeResponseBodyAsStream: Returns a handle to the stream representing the response body. The request must be paused in the HeadersReceived stage. Note that after this command the request can't be continued as is -- client either needs to cancel it or to provide the response body. The stream only supports sequential read, IO.read will fail if the position is specified. This method is mutually exclusive with getResponseBody. Calling other methods that affect the request or disabling fetch domain before body is received results in an undefined behavior.

提取域"事件如下:

  • Fetch.requestPaused:在启用域并且请求URL与指定的过滤器匹配时发出.该请求将暂停,直到客户端以continueRequest,failRequest或fulfillRequest之一进行响应.可以通过responseErrorReason和responseStatusCode的存在来确定请求的阶段-如果存在这些字段中的任何一个,则请求处于响应阶段,否则处于请求阶段.
  • Fetch.authRequired:当将handleAuthRequests设置为true启用域时发出.该请求将暂停,直到客户端用continueWithAuth响应为止.
  • Fetch.requestPaused: Issued when the domain is enabled and the request URL matches the specified filter. The request is paused until the client responds with one of continueRequest, failRequest or fulfillRequest. The stage of the request can be determined by presence of responseErrorReason and responseStatusCode -- the request is at the response stage if either of these fields is present and in the request stage otherwise.
  • Fetch.authRequired: Issued when the domain is enabled with handleAuthRequests set to true. The request is paused until client responds with continueWithAuth.

您可以在以下地方找到一些有趣的讨论

You can find a couple of revelant discussions in:

  • Can Selenium WebDriver (java) interact with the Browser's inspect tool element selector?
  • What is the difference between WebDriver and DevTool protocol

这篇关于如何使用Selenium中的Chrome DevTools协议(使用Python)来捕获HTTP请求和响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆