如何使用 python selenium 获取浏览器网络日志

问题描述

我正在尝试使用 selenium 获取浏览器网络日志以调试请求/响应.你能帮我找出一个方法吗.

I'm trying to get browser network logs using selenium to debug request/responses. Could you please help me to find out a way.

我正在使用 selenium 3.14.0 和最新的 Chrome 浏览器.

And I'm using selenium 3.14.0 and latest Chrome browser.


解决方案

使用python + selenium + firefox

除非必须,否则不要设置代理——为了获得出站 API 请求,我使用了这个答案中的解决方案,但在 python 中:https://stackoverflow.com/a/45859018/14244758

Don't set up a proxy unless you have to- in order to get outbound API requests I used the solution from this answer, but in python: https://stackoverflow.com/a/45859018/14244758

test = driver.execute_script("var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;")

for item in test:
  print(item)

你得到一个字典数组.

这让我可以看到所有的网络请求.我正在使用它从其中一个请求中解析出一个参数,以便我可以使用它来针对 API 发出自己的请求.

This allows me to see all the network requests made. I'm using it to parse out a parameter from one of the requests so that I can use it to make my own requests against the API.

相关文章