How to send an http RequestHeader using Selenium 2?

后端 未结 4 1274
误落风尘
误落风尘 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 22:51

    Unfortunately you can not change headers with Selenium 2. This has been a conscious decision on the teams part as we are trying to create a browser automation framework that emulates what a user can do.

    0 讨论(0)
  • 2020-12-09 22:52

    I encountered the same issue 2 weeks ago. I tried the approach suggested by Carl but it seemed a bit to much "overhead" to realize this task.

    In the end I used the fiddlerCore library in order to host a proxy server in my code and just use the build in proxy feature of web driver 2. It works pretty good and is much more intuitive/stable in my opinion. Furthermore it works for all browsers and you don't depend on binary files which you need to maintain inside your code repository.

    Here an example made in C# for Chrome (Firefox and IE are very similar)

    // Check if the server is already running
    if (!FiddlerApplication.IsStarted())
    {
        // Append your delegate to the BeforeRequest event
        FiddlerApplication.BeforeRequest += delegate(Fiddler.Session oS)
        {
            // Make the modifications needed - change header, logging, ...
            oS.oRequest["SM_USER"] = SeleniumSettings.TestUser;
        };
        // Startup the proxy server
        FiddlerApplication.Startup(SeleniumSettings.ProxyPort, false, false);
    }
    
    // Create the proxy setting for the browser
    var proxy = new Proxy();
    proxy.HttpProxy = string.Format("localhost:{0}", SeleniumSettings.ProxyPort);
    
    // Create ChromeOptions object and add the setting
    var chromeOptions = new ChromeOptions();
    chromeOptions.Proxy = proxy;
    
    // Create the driver
    var Driver = new ChromeDriver(path, chromeOptions);
    
    // Afterwards shutdown the proxy server if it's running
    if (FiddlerApplication.IsStarted())
    {
        FiddlerApplication.Shutdown();
    }
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-12-09 23:08

    (This example is done using Chrome)

    This is how I fixed the problem in my case. Hopefully, might be helpful for anyone with a similar setup.

    1. Add the ModHeader extension to the chrome browser

    How to download the Modheader? Link

    ChromeOptions options = new ChromeOptions();
    options.addExtensions(new File(C://Downloads//modheader//modheader.crx));
    
    // Set the Desired capabilities 
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    
    // Instantiate the chrome driver with capabilities
    WebDriver driver = new RemoteWebDriver(new URL(YOUR_HUB_URL), options);
    
    1. Go to the browser extensions and capture the Local Storage context ID of the ModHeader

    1. Navigate to the URL of the ModHeader to set the Local Storage Context

    .

    // set the context on the extension so the localStorage can be accessed
    driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/_generated_background_page.html");
    
    Where `idgpnmonknjnojddfkpgkljpfnnfcklj` is the value captured from the Step# 2
    
    1. Now add the headers to the request using Javascript

    .

       ((Javascript)driver).executeScript(
             "localStorage.setItem('profiles', JSON.stringify([{  title: 'Selenium', hideComment: true, appendMode: '', 
                 headers: [                        
                   {enabled: true, name: 'token-1', value: 'value-1', comment: ''},
                   {enabled: true, name: 'token-2', value: 'value-2', comment: ''}
                 ],                          
                 respHeaders: [],
                 filters: []
              }]));");
    

    Where token-1, value-1, token-2, value-2 are the request headers and values that are to be added.

    1. Now navigate to the required web-application.

      driver.get("your-desired-website");

    0 讨论(0)
提交回复
热议问题