How to set Proxy in Firefox using Selenium

HOME

FirefoxOptions options = new FirefoxOptions();
options.addPreference("network.proxy.type", 1);
options.addPreference("network.proxy.http", proxyAddress);
options.addPreference("network.proxy.http_port", proxyPort);

driver = new FirefoxDriver(options);

package org.example;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class FireFoxProxyDemo {


    public static void main(String[] args) {

        // Set the proxy server details
        String proxyAddress = "localhost";
        int proxyPort = 8080;
        FirefoxDriver driver;

        // Create an instance of `FirefoxOptions` and set the proxy configuration
        FirefoxOptions options = new FirefoxOptions();
        options.addPreference("network.proxy.type", 1);
        options.addPreference("network.proxy.http", proxyAddress);
        options.addPreference("network.proxy.http_port", proxyPort);

        // Instantiate FireFox Driver with the configured options
         driver = new FirefoxDriver(options);

        // Perform your browsing actions using the driver
        driver.get("https://www.google.com");
        System.out.println("Page Title :" + driver.getTitle());

        // Close the browser session
        driver.quit();
    }
}

How to set Proxy in Chrome using Selenium

HOME

     // Set the proxy server details
        String proxyAddress = "proxy.example";
        int proxyPort = 8080;

        // Create a Proxy object and set the HTTP proxy details
        Proxy proxy = new Proxy();
        proxy.setHttpProxy(proxyAddress + ":" + proxyPort);

ChromeOptions options = new ChromeOptions();
options.setProxy(proxy);
WebDriver driver = new ChromeDriver(options);

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ProxyDemo {

    public static void main(String[] args) {

        // Set the proxy server details
        String proxyAddress = "localhost";
        int proxyPort = 8080;

        // Create a Proxy object and set the HTTP proxy details
        Proxy proxy = new Proxy();
        proxy.setHttpProxy(proxyAddress + ":" + proxyPort);

        // Configure Chrome options with the Proxy object
        ChromeOptions options = new ChromeOptions();
        options.setProxy(proxy);
        options.addArguments("start-maximized");

        // Instantiate ChromeDriver with the configured options
        WebDriver driver = new ChromeDriver(options);


        // Perform your browsing actions using the driver
        driver.get("https://www.google.com");
        System.out.println("Page Title :" + driver.getTitle());

        // Close the browser session
        driver.quit();
    }
}