Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to capture network traffic using proxy in selenium webdriver code

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();
         }
}
like image 263
Lingaraj R M Avatar asked Jun 25 '26 20:06

Lingaraj R M


2 Answers

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.

like image 51
ankushagarwal Avatar answered Jun 27 '26 10:06

ankushagarwal


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

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

Examples here

like image 42
Lukasz A Avatar answered Jun 27 '26 11:06

Lukasz A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!