setting request headers in selenium

后端 未结 9 1184
青春惊慌失措
青春惊慌失措 2020-11-29 21:53

I\'m attempting to set the request header \'Referer\' to spoof a request coming from another site. We need the ability test that a specific referrer is used, which returns a

9条回答
  •  遥遥无期
    2020-11-29 22:57

    With the solutions already discussed above the most reliable one is using Browsermob-Proxy

    But while working with the remote grid machine, Browsermob-proxy isn't really helpful.

    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");

提交回复
热议问题