0
votes

I want to set proxy username and password in firefox using selenium in java. This is my code:

public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
        WebDriver driver = null;
        try {
            String PROXY_HOST = "**********";
            int PROXY_PORT = ****;
            String USERNAME = "******";
            String PASSWORD = "******";
            ProfilesIni profileIni = new ProfilesIni();
            FirefoxProfile profile = profileIni.getProfile("test");
            profile.setPreference("network.proxy.type", 1);
            profile.setPreference("network.proxy.http", PROXY_HOST);
            profile.setPreference("network.proxy.http_port", PROXY_PORT);
            profile.setPreference("network.proxy.ssl", PROXY_HOST);
            profile.setPreference("network.proxy.ssl_port", PROXY_PORT);
            FirefoxOptions options = new FirefoxOptions();
            options.setProfile(profile);
            driver = new FirefoxDriver(options);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            WebDriverWait wait = new WebDriverWait(driver, 10); 
            String parentWindowHandler = driver.getWindowHandle();
            for (String currentWindow: driver.getWindowHandles())
                   driver.switchTo().window(currentWindow);
            Thread.sleep(1000);
            autoType(USERNAME);
            Thread.sleep(1000);
            autoTab();
            Thread.sleep(1000);
            autoType(PASSWORD);
            Thread.sleep(1000);
            autoSubmit();
            Thread.sleep(1000);
            driver.switchTo().window(parentWindowHandler);
            Thread.sleep(1000);
            driver.get("http://ident.me");
        } catch (Exception e2) {
            System.out.println("Error: " + e2);
        }finally {
        if(driver != null)
            driver.close();
        }
    }

    private static void autoType(String string) {
        Robot robot;
        try {
            robot = new Robot();
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            StringSelection stringSelection = new StringSelection(string);
            clipboard.setContents(stringSelection, null);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
        } catch (AWTException e) {
            System.out.println("autoType Error: " + e);
        }
    }

    private static void autoTab() {
        Robot robot;
        try {
            robot = new Robot();
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
        } catch (AWTException e) {
            System.out.println("autoTab Error: " + e);
        }
    }

    private static void autoSubmit() {
        Robot robot;
        try {
            robot = new Robot();
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        } catch (AWTException e) {
            System.out.println("autoSubmit Error: " + e);
        }
    }

The code loads firefox profile and set proxy host and proxy port. Then it creates firefox driver and opens new firefox window. When authentication prompt opens it enters username and password and submits them but it could not load the url and throws error below:

Error: org.openqa.selenium.WebDriverException: TypeError: this.tabModal is null
1
if you are using Basic authentication (which I assume you are), you can try to set the header in all requests. I answered it in another thread in Selenium git: github.com/SeleniumHQ/selenium/issues/1802 - Adi Ohana

1 Answers

0
votes

The short answer is you cant do it easily (sometimes at all). There are tons of work around's, but everyone I have tried only works part of the time or requires tons of configuration with every new browser you want to add.

The only solution I have found to work very consistently is to use Luminati Proxy manager (LPM). Its allows you to make a password protected proxy open on your local network using their open source software.

It seems like a giant pain to setup a separate thing but the only way I got consistently working.