8

I was trying to use addCustomRequestHeader method to set a custom header for selenium requests. Given below is the source code

 Selenium sel = new DefaultSelenium("localhost",4444,"*firefox","http://www.google.com"); sel.start("addCustomRequestHeader=true"); // sel.start(); sel.addCustomRequestHeader("mycustomheader","automation"); sel.open("http://www.google.com/"); 

This code didn't add the header to the request. I tried to look up the request headers using Fiddler. Does any one here know what am I doing wrong here? Any help would be appreciated

2
  • Does it work for any custom header or only supported known HTTP headers? That is, perhaps it behaves more like an addRequestHeader().
    – David
    CommentedMar 7, 2012 at 0:58
  • Try by setting Selenium as a proxy server and it is discussed here stackoverflow.com/questions/4442405/…
    – Dhivya
    CommentedAug 29, 2012 at 10:14

2 Answers 2

3

You need to start the selenium in proxy injection mode

java -jar selenium-server-standalone.jar -proxyInjectionMode 

You can then add custom requests headers like this (in Python)

sel.start("addCustomRequestHeader=true") sel.add_custom_request_header("mycustomheader","automation") sel.open('http://www.google.com') 

To see if the custom header has been applied, check the tab that has the selenium server running. You should see something like this in the console messages

INFO - Command request: addCustomRequestHeader[mycustomheader, automation] on session INFO - Got result: OK on session 
    1

    In my case, running selenium in proxy injection mode is not acceptable so I have followed the approach of 'ModHeader' chrome extension to set the custom headers. This approached worked fine for me.

    ModHeader Extension: https://github.com/mdamien/chrome-extensions-archive 

    Here is the code snippet

    ChromeOptions chromeOpt = new ChromeOptions(); chromeOpt.addArguments("--no-sandbox"); System.setProperty("webdriver.chrome.args", "--disable-logging"); System.setProperty("webdriver.chrome.silentOutput", "true"); chromeOpt.addExtensions(new File("./ModHeader_v2.2.3.crx")); WebDriver driver = new ChromeDriver(chromeOpt); // set the context on the extension so the localStorage can be accessed driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/icon.png"); // setup ModHeader with two headers (token1 and token2) ((JavascriptExecutor)driver).executeScript( "localStorage.setItem('profiles', JSON.stringify([{ " + " title: 'Selenium', hideComment: true, appendMode: '', " + " headers: [ " + " {enabled: true, name: 'token1', value: 'testHeaderValue1', comment: ''}, " + " {enabled: true, name: 'token2', value: 'testHeaderValue2', comment: ''} " + " ], " + " respHeaders: [], " + " filters: [] " + "}])); "); driver.navigate().to("https://localhost:8443"); 

    Fiddler Custom header snapshot Fiddler Custom header snapshot

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.