1

I want to add custom http request headers to Selenium PhantomJS. Specifically this header Accept-Language: en-US

I was able to figure this much out by reading other posts, but how would I fill out the second parameter of setCapability to set a custom header?

DesiredCapabilities caps = new DesiredCapabilities();       
caps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX, "");

I've seen the other post on Stackoverflow but that one is related to Python and not Java.

Arya
  • 8,473
  • 27
  • 105
  • 175

1 Answers1

3

The PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX constant = 'phantomjs.page.customHeaders.'

So you need to add the header to the prefix.

In your case you want to do this:

DesiredCapabilities cap = DesiredCapabilities.phantomjs();
cap.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", "en-US");
cap.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "path to phantomjs executable");

Then simply pass the capabilities to the driver's constructor:

WebDriver driver = new PhantomJSDriver(cap);
Bram
  • 127
  • 1
  • 2
  • 7