How Do I Reset The Har File Used By Browser-Mob-Proxy Module For Python?

孤人 提交于 2021-01-29 17:37:03

问题


i'm using the browser-mob-proxy module and selenium for python in order to record some things that are sent by a website when you are using it.

What I am doing is creating a server using browser-mob-proxy, then creating a proxy for that server. I then create a HAR to record the data. I later use this data for something else.

I want to know if there is a way to reset the HAR file so that it is empty or if I will have to create a new HAR to store new data.

The proxy is assigned to the selenium browser which is using the chrome driver.


回答1:


I do this in my testing framework so that each test has its own HAR file for debugging purposes. Even when they use the same browser.

The command you are looking for is "new_har". This creates a new session and begins logging to a new HAR file. You can also specify a name for the session. I normally get the old HAR first and save it before clearing and starting a new session. But you don't have to do that if all you want to do is clear the proxy log.

Here is an example using the Python module.

from browsermobproxy import Server
server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()

from selenium import webdriver
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)

proxy.new_har("google") # Start first session
driver.get("http://www.google.co.uk")
proxy.har # returns a HAR JSON blob for first session

proxy.new_har("Yahoo") # Start second session
driver.get("http://www.yahoo.co.uk")
proxy.har # returns a HAR JSON blob for second session

server.stop()
driver.quit()


来源:https://stackoverflow.com/questions/56742167/how-do-i-reset-the-har-file-used-by-browser-mob-proxy-module-for-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!