Skip to content

Instantly share code, notes, and snippets.

@sureshsarda
Created October 4, 2018 01:25
Show Gist options
  • Save sureshsarda/d0553d8d3b63c3d301610a108c9e2c27 to your computer and use it in GitHub Desktop.
Save sureshsarda/d0553d8d3b63c3d301610a108c9e2c27 to your computer and use it in GitHub Desktop.
Download the network tab content when loading a page
import time
import pprint
from selenium import webdriver
from browsermobproxy import Server
class Browser:
def __init__(self):
self.server = None
self.profile = None
self.driver = None
self.proxy = None
self.options = {'port': 8090}
def start(self):
self.server = Server('/home/suresh/tools/browsermob-proxy-2.1.4/bin/browsermob-proxy', options=self.options)
self.server.start()
time.sleep(1)
self.proxy = self.server.create_proxy()
time.sleep(1)
service_args = ["--proxy-server=%s" % self.proxy.proxy]
self.profile = webdriver.FirefoxProfile()
selenium_proxy = self.proxy.selenium_proxy()
self.profile.set_proxy(selenium_proxy)
self.driver = webdriver.Firefox(firefox_profile=self.profile)
def stop(self):
self.server.stop()
self.driver.quit()
if __name__ == '__main__':
b = Browser()
b.start()
b.proxy.new_har("google")
b.driver.get("http://sureshsarda.in")
pprint.pprint(b.proxy.har, indent=4) # returns a HAR JSON blob
b.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment