How to send an http RequestHeader using Selenium 2?

后端 未结 4 1273
误落风尘
误落风尘 2020-12-09 22:25

I needed to send an Http request with a few modified headers. After several hours trying to find an equivalent method to that of Selenium RC Selenium.addCustomRequestH

4条回答
  •  醉梦人生
    2020-12-09 23:03

    As per Alberto's answer you can add modify headers to the Firefox profile if you are using it:

    FirefoxDriver createFirefoxDriver() throws URISyntaxException, IOException {
        FirefoxProfile profile = new FirefoxProfile();
        URL url = this.getClass().getResource("/modify_headers-0.7.1.1-fx.xpi");
        File modifyHeaders = modifyHeaders = new File(url.toURI());
    
        profile.setEnableNativeEvents(false);
        profile.addExtension(modifyHeaders);
    
        profile.setPreference("modifyheaders.headers.count", 1);
        profile.setPreference("modifyheaders.headers.action0", "Add");
        profile.setPreference("modifyheaders.headers.name0", SOME_HEADER);
        profile.setPreference("modifyheaders.headers.value0", "true");
        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);
        return new FirefoxDriver(capabilities);
    }
    

提交回复
热议问题