Need to capture network traffic using proxy in selenium webdriver code

11,740

Solution 1

The latest version of Selenium Webdriver does not really support traffic capture. You can however, use BrowserMob proxy to capture traffic. https://github.com/lightbody/browsermob-proxy . The README has examples on how to do that with Selenium.

Solution 2

Similar topic: https://stackoverflow.com/a/55202231/2917470

You can try moxproxy to capture or modify http traffic - github repo

Examples here

Share:
11,740
Lingaraj R M
Author by

Lingaraj R M

Updated on June 05, 2022

Comments

  • Lingaraj R M
    Lingaraj R M almost 2 years

    Need capture network traffic using proxy in selenium webdriver code.. I've tried with below code but after opening browser google.com is not loading getting the error "proxy server that is refusing connections"

    public class Test_One {
    
    /**
    * @param args
    * @throws Exception
    */
    public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    ProxyServer server = new ProxyServer(8090);
             server.start();
             server.setCaptureHeaders(true);
    
             server.setCaptureContent(true);
             server.newHar("test");
             DesiredCapabilities capabilities = new DesiredCapabilities();
             Proxy proxy = server.seleniumProxy();
             FirefoxProfile profile = new FirefoxProfile();
             profile.setAcceptUntrustedCertificates(true);
             profile.setAssumeUntrustedCertificateIssuer(true);
             profile.setPreference("network.proxy.http", "localhost");
             profile.setPreference("network.proxy.http_port", 8090);
             profile.setPreference("network.proxy.ssl", "localhost");
             profile.setPreference("network.proxy.ssl_port", 8090);
             profile.setPreference("network.proxy.type", 1);
             profile.setPreference("network.proxy.no_proxies_on", "");
             profile.setProxyPreferences(proxy);
             capabilities.setCapability(FirefoxDriver.PROFILE,profile);
             capabilities.setCapability(CapabilityType.PROXY, proxy);
             WebDriver driver = new FirefoxDriver(capabilities);
             driver.get("http://www.google.com");
             Har har1 = server.getHar();
             server.stop();
             driver.quit();
             }
    }