How to handle proxy in Selenium in Java?


We can handle proxy in Selenium in Java with the help of PROXY class.

import java.io.IOException;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class ProxySelJav {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      WebDriver driver;
      String prox = "localhost:8080";
      // set browser settings with Desired Capabilities
      Proxy p = new Proxy(); p.setHttpProxy(prox).setFtpProxy(prox).setSslProxy(prox)
      .setSocksProxy(prox);
      DesiredCapabilities c = new DesiredCapabilities();
      c.setCapability(CapabilityType.PROXY, p);
      // utilize capabilities on launching browser driver
      driver = new Firefox(c);
   }
}

Updated on: 11-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements