close

How to get request headers in Selenium

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to get request headers in Selenium in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to get request headers in Selenium?

  1. How to get request headers in Selenium?

    You can use Selenium Wire. It is a Selenium extension which has been developed for this exact purpose.

  2. get request headers in Selenium

    You can use Selenium Wire. It is a Selenium extension which has been developed for this exact purpose.

Method 1

You can use Selenium Wire. It is a Selenium extension which has been developed for this exact purpose.

An example after pip install:

##  Import webdriver from Selenium Wire instead of Selenium
from seleniumwire import webdriver

##  Get the URL
driver = webdriver.Chrome("my/path/to/driver", options=options)
driver.get("https://my.test.url.com")

##  Print request headers
for request in driver.requests:
  print(request.url) # <--------------- Request url
  print(request.headers) # <----------- Request headers
  print(request.response.headers) # <-- Response headers

Method 2

You can run JS command like this;

var req = new XMLHttpRequest()
req.open('GET', document.location, false)
req.send(null)
return req.getAllResponseHeaders()

On Python;

driver.get("https://t.me/codeksiyon")
headers = driver.execute_script("var req = new XMLHttpRequest();req.open('GET', document.location, false);req.send(null);return req.getAllResponseHeaders()")

# type(headers) == str

headers = headers.splitlines()

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read