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();
    }
}