How to set Proxy in Firefox using Selenium WebDriver?



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();    } }
Updated on: 2021-06-29T08:21:57+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements