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

Ana*_*d S 6 python selenium selenium-chromedriver selenium-webdriver

我正在尝试使用 selenium 获取浏览器网络日志来调试请求/响应。你能帮我找到一种方法吗?

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

gra*_*ntr 9

使用 python + selenium + firefox

除非必须,否则不要设置代理 - 为了获得出站 API 请求,我使用了这个答案中的解决方案,但在 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)
Run Code Online (Sandbox Code Playgroud)

你会得到一个字典数组。

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


Nav*_*asu 5

使用 Python 和 ChromeDriver

要获取网络日志,您还需要在 python 中安装BrowserMobProxy和 selenium

pip install browsermob-proxy
Run Code Online (Sandbox Code Playgroud)

然后我们需要从https://bmp.lightbody.net/下载 browsermobproxy zip

将其解压缩到任何文件夹(例如 path/to/extracted_folder)。此文件夹包含 browsermob-proxy 二进制文件。我们需要在python代码中调用Server()时提到这个路径

您需要启动浏览器代理并在chrome驱动程序的chrome选项中配置代理,

from browsermobproxy import Server
from selenium import webdriver

server = Server("path/to/extracted_folder/bin/browsermob-proxy")
server.start()
proxy = server.create_proxy()

# Configure the browser proxy in chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
browser = webdriver.Chrome(chrome_options = chrome_options)

#tag the har(network logs) with a name
proxy.new_har("google")
Run Code Online (Sandbox Code Playgroud)

然后你可以使用 selenium 导航到页面

browser.get("http://www.google.co.in")
Run Code Online (Sandbox Code Playgroud)

导航后,可以从代理获取json格式的网络日志

print(proxy.har) # returns a Network logs (HAR) as JSON 
Run Code Online (Sandbox Code Playgroud)

同样在退出驱动程序之前,最后也停止代理服务器,

server.stop()
browser.quit()
Run Code Online (Sandbox Code Playgroud)


Geo*_*zes -4

要仅获取网络日志直到页面完成加载(页面主要使用期间没有ajax/异步网络日志),您可以获取性能日志:http ://chromedriver.chromium.org/logging/performance-日志

例如,要启用 ChromeDriver 的性能日志记录,

DesiredCapabilities cap = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), cap);
Run Code Online (Sandbox Code Playgroud)

chromium性能日志页面还链接到这个完整的示例https://gist.github.com/klepikov/5457750,其中包含用于获取性能日志的 Java 和 python 代码。

再次强调,请务必记住,这只会在页面加载完成之前发出网络请求。之后,驱动程序将仅返回相同的性能日志,直到页面重新加载。


如果您希望在页面使用过程中异步获取网络日志,您可以使用BrowserMobProxy充当 Selenium 驱动程序的代理服务器并捕获所有这些网络请求。然后,您可以从 BrowserMobProxy 生成的 HAR 文件中获取这些捕获的请求:https://github.com/lightbody/browsermob-proxy#using-with-selenium

// start the proxy
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);

// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

// start the browser up
WebDriver driver = new FirefoxDriver(capabilities);

// enable more detailed HAR capture, if desired (see CaptureType for the complete list)
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

// create a new HAR with the label "yahoo.com"
proxy.newHar("yahoo.com");

// open yahoo.com
driver.get("http://yahoo.com");

// get the HAR data
Har har = proxy.getHar();
Run Code Online (Sandbox Code Playgroud)

获得 HAR 文件后,它是一个类似于 JSON 的网络事件列表,您可以使用它。