Fetch browser token in python

Issue

I want to get Auth token from browser, I am using selenium to make browser instance. I want to fetch the tokens in the network tab. How do I get it?

Solution

Selenium has a build in method to get all the network details from the browser instance. To use it you have to use ‘driver.requests’; this will give you the list of all the network requests the browser instance has made and all the headers the browser has received. If you want token with any name just replace "name_of_token_you_you_want" with the name of the token you want in inverted comma.

But, before you get the token, you have to confirm the token exists. This is why I added line 3, to confirm the token exists and then getting the token.

If you know what URL the token will have you can have an and statement to be sure the token is correct.

You need to change type_of_method_you_want to "POST" or "GET", depending on type of token you want.

You also have to change request_url_you_want to a URL your token will send or receive to or from.

for request in driver.requests:
    if request.method == "type_of_method_you_want" and request.url == "request_url_you_want":
        if request.headers.get("name_of_token_you_want"): #to confirm it exists before assigning a variable to it.
            token = request.headers.get("name_of_token_you_want")
            print(token)

Answered By – wraient

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published