Using selenium 4.12 in Python, how can I intercept an HTTP request to see what its body or headers look like?
Please Note, that I'm not asking for code but rather for resources/ideas of different or suggested methods to do so. As most of the resources refer to old versions of Selenium and I read a lot has changed in relation to this task in version 4.
I know there is the option to turn on wireshark but is there something to do with Python or Selenium itself?
Please Note: I have written a huge number of lines using selenium so I prefer a method in which I don't need to fully rewrite my code or with minimal effort.
If you need just to get request / response data without modifying it, Selenium 4 has a feature that logs network events.
You just need to enable capability 'goog:loggingPrefs', {'performance': 'ALL'}
and then call driver.get_log("performance")
when you expect that needed request was performed.
To get event data, you can execute commands from Chrome DevTools Protocol.
For example, this code gets response body for defined request with url part.
from selenium import webdriver
import jsonoptions = webdriver.ChromeOptions()
options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
driver = webdriver.Chrome(options=options)
driver.get('url')logs = driver.get_log("performance")target_url = 'your_target_url_part'for log in logs:message = log["message"]if "Network.responseReceived" in message:params = json.loads(message)["message"].get("params")if params:response = params.get("response")if response:if response and target_url in response["url"]:body = driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': params["requestId"]})response_body = body['body']
Example of usage:
from selenium.webdriver.common.by import By
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import json
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
options = webdriver.ChromeOptions()options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument('--start-maximized')
options.add_argument('--disable-extensions')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(options=options)driver.get("https://google.com")
wait = WebDriverWait(driver, 15)
text_area = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'textarea[class]')))
action_chains = ActionChains(driver)
action_chains.click(text_area).perform()
search_input = 'Network Interceptor'
# this block is written to emulate user typing, so it would be sent each search request per letter
for i in range(len(search_input)):action_chains.send_keys(search_input[i]).perform()time.sleep(0.3)
target_url = 'search'
logs = driver.get_log("performance")def get_search_repsonses():for log in logs:message = log["message"]if "Network.responseReceived" in message:params = json.loads(message)["message"].get("params")if params:response = params.get("response")if response:if response and target_url in response["url"]:body = driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': params["requestId"]})print(body)print('____________________________________________')get_search_repsonses()