Chrome DevTools Protocol in Python
Using this and PyChrome, you can read network requests in Selenium.
Oct 24, 2021
It might not sound like it, but this is actually really cool. Why would you want to read network requests tho? You can use them to find endpoints.
Use this:
import pychrome
from selenium import webdriveroptions = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/Users/davidshivaji/Library/Application Support/Google/Chrome/Profile 7")
options.add_argument("--remote-debugging-port=8000")
chromedriver = webdriver.Chrome(chrome_options=options, executable_path='/Users/davidshivaji/chromedriver')def outputstart(**kwargs):
print("start ", kwargs)
driver = chromedriver
dev_tools = pychrome.Browser(url="http://localhost:8000")
tab = dev_tools.list_tab()[0]
tab.start()
url = 'https://google.com'
start = time.time()
driver.get(url)
tab.call_method("Network.emulateNetworkConditions",
offline=False,
latency=100,
downloadThroughput=93750,
uploadThroughput=31250,
connectionType="wifi")
tab.call_method("Network.enable", _timeout=20)
tab.set_listener("Network.requestWillBeSent", outputstart)
This will last for 20 seconds and you’ll receive everything you’d normally see in the network tab, printed to the console.
From there, you can get response bodies & more information from each request.