Skip to content

Instantly share code, notes, and snippets.

@l50
Last active May 27, 2020 23:39
Show Gist options
  • Save l50/ea0979bb68df213b799fdc5c2dfe6a85 to your computer and use it in GitHub Desktop.
Save l50/ea0979bb68df213b799fdc5c2dfe6a85 to your computer and use it in GitHub Desktop.
Selenium+Chrome+BrowserMob-Proxy
from browsermobproxy import Server
from selenium import webdriver
import time
import pprint
class ProxyManager:
# You may need to change this, depending on which version of browsermob-proxy you have and where it is located
__BMP = "./assets/browsermob-proxy-2.1.4/bin/browsermob-proxy"
def __init__(self, port):
self.__port = port
self.__server = Server(ProxyManager.__BMP, options={
'port': self.__port})
self.__client = None
def start_server(self):
self.__server.start()
return self.__server
def start_client(self):
self.__client = self.__server.create_proxy(
params={"trustAllServers": "true"})
return self.__client
@property
def client(self):
return self.__client
@property
def server(self):
return self.__server
if __name__ == '__main__':
proxy = ProxyManager(8095)
server = proxy.start_server()
client = proxy.start_client()
client.new_har('google.com', options={
'captureHeaders': True, 'captureContent': True})
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server={0}".format(client.proxy))
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://google.com/')
# Wait 3 seconds for the page to load
time.sleep(3)
pprint.pprint(client.har)
server.stop()
driver.quit()
# Be sure to download the latest version of browsermob-proxy from here: https://github.com/lightbody/browsermob-proxy/releases
# This particular code has it living in ./assets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment