Headers and Selenium Webdriver 2
Asked Answered
V

4

4

Is there any way to add a header in Selenium WebDriver test? (Like with Firefox Modify Headers plugin) I cannot use HtmlUnitDriver, because the browser has to be visible.

Vastitude answered 24/10, 2012 at 10:17 Comment(1)
For those people using Python to author their Selenium tests, you might consider Selenium Wire which gives you the ability to add headers sent by the browser, as well as inspect requests and responses.Dobsonfly
B
3

WebDriver does not allow you to change or set the headers using any of the browser based drivers. You can find a lot of information about their decision about the headers and the response codes at the following URL. http://code.google.com/p/selenium/issues/detail?id=141

We use Apache HTTP Client for this type of testing where we do not want to check the rendered page elements, but just the responses and header information.

You can also give browser mob proxy with your selenium tests as well as mentioned in the url above. I have used this for other purposes and it is awesome.

Baikal answered 24/10, 2012 at 22:23 Comment(0)
G
1

Here is a short example of how to use Seleniumwire with Python:

from seleniumwire import webdriver

def set_chrome_driver():
        options = webdriver.ChromeOptions()
        options.add_argument("--start-maximized")
        options.add_argument("--disable-infobars")
        options.add_argument("--no-proxy-server")
        driver = webdriver.Chrome(executable_path=r'C:\Automation_package\chromedriver.exe')
        driver.get('http://172.1.1.1:5000/path/api/')
        driver.header_overrides = {"iv-user": "Admin", "iv-groups": "SuperAdmin", "iv-roles": "Viewers",}
        driver.get('http://172.1.1.1:5000/path/api/')
Gallman answered 10/6, 2021 at 16:52 Comment(0)
K
1

You can achieve that by making use of a remote-contolled extension using the chrome.declarativeNetRequest api. As it doesn't use any proxies or IO, the network speed won't reduce noticeably.

Selenium-Injector provides an implementation for that.

Install with:

pip install selenium_injector

Example script:

from selenium_injector.webdriver import Chrome

driver = Chrome()

# modify headers
driver.injector.declarativeNetRequest.update_headers({"test": "test_2", "sec-ch-ua-platform": "Android"})

driver.get("https://httpbin.org/headers")

input("press ENTER to exit")
driver.quit()

Disclaimer: I am the author of Selenium-Injector

Klein answered 4/8, 2023 at 18:13 Comment(0)
S
0

There are alternative methods to do this like Browsermob-Proxy

Since Browsermob-proxy has its own limitations while we work on the selenium grid, the below is how I fixed the problem in my case. Hopefully, might be helpful for anyone with a similar setup.

  1. Add the ModHeader extension to the chrome browser

How to download the Modheader? Link

ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(C://Downloads//modheader//modheader.crx));

// Set the Desired capabilities 
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

// Instantiate the chrome driver with capabilities
WebDriver driver = new RemoteWebDriver(new URL(YOUR_HUB_URL), options);
  1. Go to the browser extensions and capture the Local Storage context ID of the ModHeader

Capture ID from ModHeader

  1. Navigate to the URL of the ModHeader to set the Local Storage Context

.

// set the context on the extension so the localStorage can be accessed
driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/_generated_background_page.html");

Where `idgpnmonknjnojddfkpgkljpfnnfcklj` is the value captured from the Step# 2
  1. Now add the headers to the request using Javascript

.

   ((Javascript)driver).executeScript(
         "localStorage.setItem('profiles', JSON.stringify([{  title: 'Selenium', hideComment: true, appendMode: '', 
             headers: [                        
               {enabled: true, name: 'token-1', value: 'value-1', comment: ''},
               {enabled: true, name: 'token-2', value: 'value-2', comment: ''}
             ],                          
             respHeaders: [],
             filters: []
          }]));");

Where token-1, value-1, token-2, value-2 are the request headers and values that are to be added.

  1. Now navigate to the required web-application.

    driver.get("your-desired-website");

Smacking answered 11/8, 2020 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.