Chirag Shah
Chirag Shah

Reputation: 373

How to handle authentication alert of browser with selenium webdriver using java without autoit and url method

I tried to set proxy with

Proxy proxy = new Proxy();
proxy.setHttpProxy("http://MY_USERNAME:MY_PASSWORD@MY_HOST:MY_PORT");

It is redirecting to specific URL but it is not actually setting proxy and giving me local IP instead MY_HOST.

I can not use autoit script.

Please guide me that how can I handle alert box

enter image description here

I have tried with driver.switchTo().alert(); but, the code is not working after the statement driver.get(MY_URL); when the popup appears.

Note : Both options (1) by set crx file and (2) by giving user name and password in URL with host and port, are authenticating successfully but please note that it is not actually set proxy as required but instead it gives local IP

Upvotes: 0

Views: 1785

Answers (2)

SKYLINE
SKYLINE

Reputation: 46

java.awt.Robot class can be used for authentication

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

login() throws Exception {
    // Pass username
    autoType(username);
    // to move to Password field
    autoTab();
    // Enter Password
    autoType(password);
    // To click on login
    autoSubmit();
}

private static void autoType(String string) throws AWTException {
    Robot 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);
}

private static void autoTab() throws AWTException {
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
}

private static void autoSubmit() throws AWTException {
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}

Upvotes: 1

Recep Duman
Recep Duman

Reputation: 123

You can create a Chrome extension that can handle the proxy on the fly. ChromeDriver does not provide any capability to handle HTTP proxy that needs credentials.

Create a zip file proxyExtension.zip that contains the following 2 files;

background.js

var config = {
    mode: "fixed_servers",
    rules: {
      singleProxy: {
        scheme: "http",
        host: "YOU_PROXY_ADDRESS",
        port: parseInt(YOUR_PROXY_PORT)
      },
      bypassList: ["foobar.com"]
    }
  };

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
    return {
        authCredentials: {
            username: "YOUR_PROXY_USERNAME",
            password: "YOUR_PROXY_PASSWORD"
        }
    };
}

chrome.webRequest.onAuthRequired.addListener(
        callbackFn,
        {urls: ["<all_urls>"]},
        ['blocking']
);

YOU_PROXY_ADDRESS, YOUR_PROXY_PORT, YOUR_PROXY_USERNAME, YOUR_PROXY_PASSWORD fields will be replaced with your informations.

manifest.json

{
    "version": "1.0.0",
    "manifest_version": 2,
    "name": "Chrome Proxy",
    "permissions": [
        "proxy",
        "tabs",
        "unlimitedStorage",
        "storage",
        "<all_urls>",
        "webRequest",
        "webRequestBlocking"
    ],
    "background": {
        "scripts": ["background.js"]
    },
    "minimum_chrome_version":"22.0.0"
}

Then, initialize the webdriver with the following code;

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File("path_to_extension_file/proxyExtension.zip"));
WebDriver driver = new ChromeDriver(chromeOptions);

Please change the path_to_extension_file to your directory that has the proxyExtension.zip file.

You can also find more information on the link.

Upvotes: 0

Related Questions