How to set Proxy in Firefox using Selenium WebDriver?

SeleniumAutomation TestingTesting Tools

We can set a proxy in Firefox using Selenium webdriver. A proxy server enables users to access an URL of an application for testing purposes even with the presence of several layers of network.

The setting up of proxy in Firefox can be done with the help of the FirefoxOptions class. The port information and the proxy server host are added to this class. The setting up of the proxy server can also be done by configuring the Firefox Profile in the FirefoxProfile class.

Example

Code Implementation with the FirefoxOptions

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class ConfigureProxy {
   public static void main(String[] args) {
      //object of Proxy
      Proxy p = new Proxy();
      //adding host and port
      p.setHttpProxy("<HOST:PORT>");
      p.setSslProxy("<HOST:PORT>");
      p.setSslProxy("<HOST:PORT>");
      p.setFtpProxy("<HOST:PORT>");
      //object of FirefoxOptions
      FirefoxOptions o = new FirefoxOptions();
      o.setCapability("proxy", p);
      //passing Firefox option to webdriver object
      WebDriver driver = new FirefoxDriver(o);
      //url launch
      driver.get("https://www.tutorialspoint.com/index.htm");
      //browser close
      driver.close();
   }
}

Example

Code Implementation with the FirefoxOptions

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class ConfigureProxyProfile {
   public static void main(String[] args) {
      //object of Proxy
      Proxy p = new Proxy();
      //adding host and port
      p.setHttpProxy("<HOST:PORT>");
      p.setSslProxy("<HOST:PORT>");
      p.setSslProxy("<HOST:PORT>");
      p.setFtpProxy("<HOST:PORT>");
      //object of FirefoxProfile
      FirefoxProfile pl = new FirefoxProfile();
      pl.setProxyPreferences(p);
      //passing Firefox profile to webdriver object
      WebDriver driver = new FirefoxDriver(pl);
      //url launch
      driver.get("https://www.tutorialspoint.com/index.htm");
      //browser close
      driver.close();
   }
}
raja
Published on 29-Jun-2021 08:20:50
Advertisements