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?
To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers.
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.
Using getText() Method To Get Heading Or Paragraph The getText() method in Selenium helps us retrieve a text and do necessary action on it.
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:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With