Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting request headers in selenium

I'm attempting to set the request header 'Referer' to spoof a request coming from another site. We need the ability test that a specific referrer is used, which returns a specific form to the user, otherwise an alternative form is given.

I can do this within poltergeist by:

page.driver.headers = {"Referer" => referer_string} 

but I can't find the equivalent functionality for the selemium driver.

How can I set request headers in the capybara selenium driver?

like image 763
tamouse Avatar asked Mar 26 '13 18:03

tamouse


People also ask

How do I add a header to a URL request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers.

How do I create a custom request header?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

How do I get header text in selenium?

Using getText() Method To Get Heading Or Paragraph The getText() method in Selenium helps us retrieve a text and do necessary action on it.


2 Answers

Webdriver doesn't contain an API to do it. See issue 141 from Selenium tracker for more info. The title of the issue says that it's about response headers but it was decided that Selenium won't contain API for request headers in scope of this issue. Several issues about adding API to set request headers have been marked as duplicates: first, second, third.

Here are a couple of possibilities that I can propose:

  1. Use another driver/library instead of selenium
  2. Write a browser-specific plugin (or find an existing one) that allows you to add header for request.
  3. Use browsermob-proxy or some other proxy.

I'd go with option 3 in most of cases. It's not hard.

Note that Ghostdriver has an API for it but it's not supported by other drivers.

like image 73
Andrei Botalov Avatar answered Sep 20 '22 04:09

Andrei Botalov


For those people using Python, you may consider using Selenium Wire which can set request headers as well as provide you with the ability to inspect requests and responses.

from seleniumwire import webdriver  # Import from seleniumwire  # Create a new instance of the Chrome driver (or Firefox) driver = webdriver.Chrome()  # Create a request interceptor def interceptor(request):     del request.headers['Referer']  # Delete the header first     request.headers['Referer'] = 'some_referer'  # Set the interceptor on the driver driver.request_interceptor = interceptor  # All requests will now use 'some_referer' for the referer driver.get('https://mysite') 

Install with:

pip install selenium-wire 
like image 36
Will Keeling Avatar answered Sep 19 '22 04:09

Will Keeling