Selenium, Intercept HTTP Request?

2024/10/6 22:22:46

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.

Answer

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()
https://en.xdnf.cn/q/118901.html

Related Q&A

Flask server returns 404 on localhost:5000 w/ Twilio

Im following this guide (Python Quickstart: Replying to SMS and MMS Messages) to try and set up a flask server, but when I try to connect to http://localhost:5000 I get a 404 error. I can ping 127.0.0.…

printing values and keys from a dictionary in a specific format (python)

I have this dictionary (name and grade):d1 = {a: 1, b: 2, c: 3}and I have to print it like this:|a | 1 | C | |b | 2 | B | |c | 3 | …

stdscr.getstr() ignore keys, just string

I just need convert entered text(bytes) to string. But if i on cyrillic press Backspace and some character, python throw me this error:UnicodeDecodeError: utf-8 codec cant decode byte 0xd0 in position …

What is wrong with the following program code, attempting to initialize a 4 x 4 matrix of integers?

What is wrong with the following program code, attempting to initialize a 4 x 4 matrix of integers? How should the initialization be done?line = [0] * 4 matrix = [line, line, line, line]

Creating a Data Pipeline to BigQuery Using Cloud Functions and Cloud Scheduler

I am trying to build a Data Pipeline that will download the data from this website and push it to a BigQuery Table. def OH_Data_Pipeline(trigger=Yes):if trigger==Yes:import pandas as pdimport pandas_gb…

Matching several string matches from lists and making a new row for each match

I have a data frame with text in one of the columns and I am using regex formatted strings to see if I can find any matches from three lists. However, when there are multiple matches from list 1, I wan…

Join and format array of objects in Python

I want to join and format values and array of objects to a string in python. Is there any way for me to do that?url = "https://google.com", search = "thai food", search_res = [{&q…

Copying text from file to specified Excel column [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

Name error: Variable not defined

Program calculates the shortest route from point, to line, then to second point. Also I need to say how long is from the start of the line, to where point crosses. My code so far: from math import sqrt…

Error while deploying flask app on apache

I have a file manage.py, import os from app import create_app app = create_app(os.getenv(FLASK_CONFIG) or default) if __name__ == __main__:app.run()manage.py is working fine when tested in debug mode. …