I'm trying to get browser network logs using selenium to debug request/responses. Could you please help me to find out a way.
And I'm using selenium 3.14.0 and latest Chrome browser.
解决方案
Using Python and ChromeDriver
To get network logs, you need to install BrowserMobProxy as well along with selenium in python
pip install browsermob-proxy
You need to start browser proxy and configure the proxy in chrome option of chrome driver,
from browsermobproxy import Server
from selenium import webdriver
server = Server("path/to/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")
Then you can navigate to page using selenium
browser.get("http://www.google.co.in")
After navigation, you can get the network logs in json format from the proxy
print(proxy.har) # returns a Network logs (HAR) as JSON
Also before quitting the driver, stop the proxy server as well at the end,
server.stop()
browser.quit()