打开Selenium中的Chrome开发者工具检查器-inspector

4

我希望能够保存(保护)一些网页的WebSocket流量。我尝试使用chrome-devtool保存HAR文件。但显然,它无法保存WebSocket流量。然而,我找到了这个答案,它建议如果我可以更改SDK并记录所有SDK.networkLog.requests()请求,则可以记录所有请求。不过,要运行SDK代码,我需要从Selenium打开chrome inspector-inspector。我知道我可以使用Selenium从这个答案中打开开发工具。有没有什么建议可以打开inspector-inspector?或者记录WebSocket的另一种方法?提前感谢!


你用的编程语言是什么? - ewwink
2个回答

3
您可以使用 wshook 来拦截和修改 WebSocket 请求和消息事件。使用 executeScript()execute_script() 将以下脚本注入到页面中。
(function() {
  window.sentMessage = [];
  window.receivedMessage = [];
  var s = document.createElement('script');
  s.src = 'https://cdn.jsdelivr.net/gh/skepticfx/wshook@0.1.1/wsHook.js';
  s.onload = function() {
    window.wsHook.before = function(data, url) {
      window.sentMessage.push(data);
      console.log("Sending message to " + url + " : " + data);
    }

    window.wsHook.after = function(messageEvent, url, wsObject) {
      window.receivedMessage.push(messageEvent.data);
      console.log("Received message from " + url + " : " + messageEvent.data);
      return messageEvent;
    }
  }
  document.body.appendChild(s)
})(); 

执行上述代码后,您可以从变量window.sentMessagewindow.receivedMessage中获取数据数组。


1

所以您可以使用Chrome远程调试协议作为解决方案。即使Selenium正在控制浏览器,其他客户端也可以连接到Chrome并进行调试。您可以使用--remote-debugging-port选项启动Chrome。我个人没有处理过记录Web套接字流量,但已经处理过一些Chrome分析,这应该能够帮助。以下是一些资源供您查阅: Chrome开发协议查看器 检查此资源是否有所帮助 chrome-remote-interface 还有这个 Selenium + Chrome Dev-tools makes a Perfect Browser Automation Recipe


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接