Changing HTTP headers for a Selenium WebDriver request

There are two ways to tackle this :

  • Using a Firefox browser extension that would modify the headers for you
  • Using a reverse proxy, like the Browser Mob Proxy

The BMP is complicated especially if you are behind a corporate proxy. So modify headers extension is the approach we can use.

Loading the extension

Just download the firefox extension, *.xpi and include that somewhere in your project. Add that to your Firefox profile as below


FirefoxProfile profile = new FirefoxProfile();
 File modifyHeaders = new File(System.getProperty("user.dir") + "/resources/modify_headers.xpi");
 profile.setEnableNativeEvents(false);
 try {
 profile.addExtension(modifyHeaders);
 } catch (IOException e) {
 e.printStackTrace();
 }

Setting the extension preferences

The next step after loading the extension, is to actually set the preferences that we need to be set. For example below, I specify that I want 1 http header to be set, the header name, the header value(which could be dynamically coming from some api call) and then finally enabling the extension. This allows the extension to be loaded when webdriver kicks of Firefox and sets it in the active mode along with the http header.

profile.setPreference(“modifyheaders.headers.count”, 1);
profile.setPreference(“modifyheaders.headers.action0”, “Add”);
profile.setPreference(“modifyheaders.headers.name0”, “sox”);
profile.setPreference(“modifyheaders.headers.value0”, “305471”);
profile.setPreference(“modifyheaders.headers.enabled0”, true);
profile.setPreference(“modifyheaders.config.active”, true);
profile.setPreference(“modifyheaders.config.alwaysOn”, true);

Setting DesiredCapabilities

To wrap it up set firefox capability to use the profile above and launch Firefox

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName(“firefox”);
capabilities.setPlatform(org.openqa.selenium.Platform.ANY);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);

WebDriver driver = new FirefoxDriver(capabilities);
driver.get(“http://website.com”);

The complete code below


@Test(invocationCount=1)
 public void launchWebsite() {
 FirefoxProfile profile = new FirefoxProfile();
 File modifyHeaders = new File(System.getProperty("user.dir") + "/resources/modify_headers.xpi");
 profile.setEnableNativeEvents(false);
 try {
 profile.addExtension(modifyHeaders);
 } catch (IOException e) {
 e.printStackTrace();
 }

profile.setPreference("modifyheaders.headers.count", 1);
 profile.setPreference("modifyheaders.headers.action0", "Add");
 profile.setPreference("modifyheaders.headers.name0", "sox");
 profile.setPreference("modifyheaders.headers.value0", "305471");
 profile.setPreference("modifyheaders.headers.enabled0", true);
 profile.setPreference("modifyheaders.config.active", true);
 profile.setPreference("modifyheaders.config.alwaysOn", true);

DesiredCapabilities capabilities = new DesiredCapabilities();
 capabilities.setBrowserName("firefox");
 capabilities.setPlatform(org.openqa.selenium.Platform.ANY);
 capabilities.setCapability(FirefoxDriver.PROFILE, profile);

WebDriver driver = new FirefoxDriver(capabilities);
 driver.get("http://website.com");
 }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s