'Add Httpheader to selenium chrome webdriver in C#
My C# code looks like this for creating chrome web driver, i wanted to add the custom HTTP headers to all my http requests.
ex: user-agent : Android
var service = ChromeDriverService.CreateDefaultService(@"c:\Chrome\");
var option = new ChromeOptions();
_driver = new ChromeDriver(service, option);
We have the way for firefox, as the link shows, but for chrome it does not work. https://eveningsamurai.wordpress.com/2013/11/21/changing-http-headers-for-a-selenium-webdriver-request/
Any help appreciated
Solution 1:[1]
I've been able to manage that using ModHeaders Chrome extension. Download the plugin CRX file and load it in your test Chrome instance.
var options = new ChromeOptions();
options.AddExtension("WebDrivers/modHeader_2_1_1.crx");
var driver = new ChromeDriver(options);
Then you can configure the plugin using the local storage as this is where the plugins stores its config too.
// set the context to access extension local storage
Configuration.driver.Navigate().GoToUrl("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/icon.png");
Configuration.driver.ExecuteScript(
"localStorage.setItem('profiles', JSON.stringify([{ " +
" title: 'Selenium', hideComment: true, appendMode: '', " +
" headers: [ " +
" {enabled: true, name: 'MY_HEADER', value: 'MY_VALUE', comment: ''} " +
" ], " +
" respHeaders: [], " +
" filters: [] " +
"}])); ");
And finally you can navigate to somewhere and check that the headers are loaded
Configuration.driver.Navigate().GoToUrl("http://example.com/");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
