Export HAR using chromedriver

后端 未结 3 722
深忆病人
深忆病人 2021-02-14 18:07

Is it possible to export HAR using chromedriver similar to what I can do with netexpert+firebug with Firefox?

3条回答
  •  悲&欢浪女
    2021-02-14 18:32

    Yes, using BrowsermobProxy you can generate HAR file using chromedriver.

    Here is a script in python to programatically generate HAR file using Selenium, BrowserMob Proxy and chromedriver. Python Packages for selenium and browsermob-proxy are needed to run this script.

    from browsermobproxy import Server
    from selenium import webdriver
    import os
    import json
    import urlparse
    
    server = Server("path/to/browsermob-proxy")
    server.start()
    proxy = server.create_proxy()
    
    chromedriver = "path/to/chromedriver"
    os.environ["webdriver.chrome.driver"] = chromedriver
    url = urlparse.urlparse (proxy.proxy).path
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--proxy-server={0}".format(url))
    driver = webdriver.Chrome(chromedriver,chrome_options =chrome_options)
    proxy.new_har("http://stackoverflow.com", options={'captureHeaders': True})
    driver.get("http://stackoverflow.com")    
    result = json.dumps(proxy.har, ensure_ascii=False)
    print result
    proxy.stop()    
    driver.quit()
    

提交回复
热议问题