有没有办法启用、暂停、恢复 chrome devtools?

问题描述 投票:0回答:1

先决条件:

  1. 在 python 中启动 selenium 脚本
  2. 启动后,我们正在尝试连接到 chrome devtools 并 控制脚本的执行以进行调试。
  3. 当 selenium 脚本开始重播时,我正在获取 websocket url 并执行下面的代码片段。

下面是我通过 python 连接到 chrome devtools 的代码片段

def connect_devtools(ws_url):
        global ws
        ws = websocket.WebSocketApp(ws_url,
                                   on_open=on_open,
                                   on_message=on_message,
                                   on_error=on_error,
                                   on_close=on_close)
        # Run WebSocket in a separate thread to keep it open
        wst = threading.Thread(target=ws.run_forever)
        wst.daemon = True
        wst.start()
        time.sleep(1)  # Wait for the connection to be established

def enable_debugger():
    enable_debugger_command = {
        "id": 1,
        "method": "Debugger.enable",
        "params": {}
    }
    ws.send(json.dumps(enable_debugger_command))

def pause_debugger():
    if ws:
        pause_command = {
            "id": 2,
            "method": "Debugger.pause",
            "params": {}
        }
        ws.send(json.dumps(pause_command))
    else:
        print("WebSocket is not connected")

def on_open(ws):
    print("WebSocket connection opened")


def on_message(ws, message):
    print("Received message: ", message)

if __name__ == "__main__":
    ws_url = "ws://127.0.0.1:35192/devtools/browser/9e552787-2329-4e7d-9cca-77a60e8d603c"  # Replace with your actual WebSocket URL
    connect_devtools(ws_url)  

 # Wait a bit before pausing to see the effect
  time.sleep(2)
  enable_debugger
  pause_debugger()

我看到下面的输出:

WebSocket connection opened
Received message:  {"id":1,"error":{"code":-32601,"message":"'Debugger.enable' wasn't found"}}
Received message:  {"id":2,"error":{"code":-32601,"message":"'Debugger.pause' wasn't found"}}

正如我们在这里看到的,websocket 连接已打开但无法暂停。

有没有办法在没有调试器的情况下启用、暂停、恢复 chrome devtools?

python selenium-webdriver websocket google-chrome-devtools
1个回答
0
投票

是的。会有一些像启用 chrome 设置之类的方法来启用、暂停和恢复 chrome DevTools。

© www.soinside.com 2019 - 2024. All rights reserved.