Seleniumwire get Response text

Question:

I’m using Selenium-wire to try and read the request response text of some network traffic. The code I have isn’t fully reproducable as the account is behind a paywall.

The bit of selenium-wire I’m currently using using is:

for request in driver.requests:
    if request.method == 'POST' and request.headers['Content-Type'] == 'application/json':
        # The body is in bytes so convert to a string
        body = driver.last_request.body.decode('utf-8')
        # Load the JSON
        data = json.loads(body)

Unfortunately though, that is reading the payload of the request
enter image description here

and I’m trying to parse the Response:
enter image description here

Asked By: JayRSP

||

Answers:

You need to get last_request‘s response:

body = driver.last_request.response.body.decode('utf-8')
data = json.loads(body)
Answered By: Yevhen Kuzmovych

I usually use these 3 steps


# I define the scopes to avoid other post requests that are not related
# we can also use it to only select the required endpoints
driver.scopes = [
     # .* is a regex stands for any char 0 or more times
    '.*stackoverflow.*',
    '.*github.*'
]

# visit the page
driver.get('LINK')

# get the response
response = driver.last_request # or driver.requests[-1]

# get the json
js = json.loads(
    decode(
        response.response.body,
        # get the encoding from the request
        response.headers.get('Content-Encoding', 'identity'),
    )
)

# this clears all the requests it's a good idea to do after each visit to the page
del driver.requests

for more info here is the doc

Answered By: Hanna