Search code examples
pythonseleniumselenium-webdriverwebdriverbrowser-automation

How to see what headers a request would contain before sending that request


So, my question may be very silly but is there a way to inspect what headers would each request contain before sending it. I am using selenium with python but the question is more shifted towards "is something like this possible" rather than "how can I do it using selenium with python"

IMPORTANT What I am looking for is a way to do that without sending the request. I know that you can easily do that after sending it but is there a way to inspect the headers of request before it being sent?


Solution

  • You can use Selenium Wire, it is an extension of Selenium which add this functionality. To read the headers before the request actually gets sent, you can use an interceptor and abort the request after you have read/processed/printed the headers

    After installing with pip (pip install selenium-wire), you can use the following:

    from seleniumwire import webdriver
    
    def interceptor(request):
        print(request.headers) # <--- Print the headers that would be sent
        request.abort() # <---------- Abort the request so that it does not get sent.
    
    driver = webdriver.Chrome()
    driver.request_interceptor = interceptor
    driver.get("https://my.test.url.com")