0

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

Chirag Shah
  • 353
  • 1
  • 13
  • Possible duplicate of [Running Selenium Webdriver with a proxy in Python](https://stackoverflow.com/questions/17082425/running-selenium-webdriver-with-a-proxy-in-python) – Todor Minakov Jan 18 '19 at 08:36
  • Have a look at the linked question (nevermind the python in the title, the approach is the same in all languages, it's just a matter of semantics). You may be missing something in your full code we are not seeing - passing the proxy object, setting the desired capabilities, etc. If you are sure your issue is not covered by *that* SO question, be sure to comment here so yours doesn't end up closed wrongly. – Todor Minakov Jan 18 '19 at 08:39
  • I go through given link, but as mentioned in my problem, It actually not set proxy however it is working with local IP. – Chirag Shah Jan 18 '19 at 08:58

2 Answers2

1

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);
}
SKYLINE
  • 46
  • 4
  • Thanks for nice solution, I implemented code as above and works only when HEADLESS chrome is set to FALSE, But it fails on HEADLESS chrome. I tried to look at background screen-shot with Web-driver, It displays blank page when HEADLESS is set to TRUE. Please guide in case of HEADLESS environment – Chirag Shah Jan 18 '19 at 07:22
  • That's the problem with `java.awt.Robot` - it simulates user's keyboard and mouse, yet - works only for a local execution, when there are visualized/rendered OS controls. Thus it is **not** a solution when controlling a remote browser - the "user" is on machine A while browser is running on B, nor for headless ones - they are not rendered in the GUI (think of them as a service), nothing to send the controls to. – Todor Minakov Jan 18 '19 at 08:33
  • Yeah that's the actual cause of not working with HEADLESS, then can you help me, How can I handle authentication popup with HEADLESS chrome to set really set and work with proxy. Except solutions are (1) using .crx file (2) username and password in URL (3) AutoIt because of linux OS – Chirag Shah Jan 18 '19 at 08:55
0

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.

Recep Duman
  • 123
  • 9
  • Rather than just a code dump, could you explain what all this actually does? – SiKing Jan 17 '19 at 20:20
  • Thank you but this solution is giving same output as URL based solution like ("http://MY_USERNAME:MY_PASSWORD@MY_HOST:MY_PORT"), i.e. working well but it is not actually set proxy as required. Tested with IP provider tool. – Chirag Shah Jan 18 '19 at 07:25
  • @Sers how did you confirm this? Please guide – Ankur Raiyani Jan 31 '19 at 11:28
  • @RecepDuman It is working only when Headless is false, when I set Headless True, it give me error "Chrome failed to start : exited abnormally". Can you help, how I can implement your code with Headless Option set to True. – Chirag Shah Feb 07 '19 at 11:45
  • @ChiragShah Chrome headless mode does not support extensions. You cannot use this approach with headless mode. – Recep Duman Feb 10 '19 at 11:53