5

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

    2 Answers 2

    10

    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/"); 
    5
    • I try to do the same in chrome, but cannot get my modheader sending the custom header. Here's the code gist.github.com/cwhsu1984/c15507c4f26433fecca2482fa51aa5a4 Can you provide me some directions to do this?
      – cwhsu
      CommentedJan 3, 2020 at 12:02
    • 1
      To bad, I get this error: org.openqa.selenium.WebDriverException: <unknown>: Failed to read the 'localStorage' property from 'Window': Access is denied for this document. (using Chrome)
      – Zoette
      CommentedMar 26, 2021 at 2:32
    • Has anyone figured out why the call to ExecuteScript gives this error? "An unhandled exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll Additional information: <unknown>: Failed to read the 'localStorage' property from 'Window': Access is denied for this document. (Session info: chrome=97.0.4692.99) occurred"
      – Ray
      CommentedJan 27, 2022 at 20:32
    • The same error OpenQA.Selenium.WebDriverException: '<unknown>: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.
      – PhiseySt
      CommentedJul 27, 2022 at 20:03
    • @PhiseySt, Cause try change the Navigate().GoToUrl... ExecuteScript... part to gist.github.com/stevenxi/c4dc7eea563d972dbccd6f7c383dc393 , assuming using latest extension version (4_0_21)
      – Steven.Xi
      CommentedOct 13, 2022 at 22:01
    3

    One way to handle this case is with FiddlerCore proxy, capture all the requests and modify the header as part of request. https://www.nuget.org/packages/FiddlerCore/

    Nice blog about fiddler core http://weblog.west-wind.com/posts/2014/Jul/29/Using-FiddlerCore-to-capture-HTTP-Requests-with-NET

     public static void Start() { FiddlerApplication.RequestHeadersAvailable += FiddlerApplication_RequestHeadersAvailable; FiddlerApplication.Startup(8888, true, true, true); } static void FiddlerApplication_RequestHeadersAvailable(Session oSession) { oSession.RequestHeaders.Add("My_Custom_Header", "XXXXXXXXXXXXXXXX"); } 
    1
    • Telerik have now unlisted this package from Nuget. The best answer is now the one posted by @guillemCommentedJul 20, 2018 at 10:43

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.