C
C
CODER_ua2015-12-14 02:03:25
Java
CODER_ua, 2015-12-14 02:03:25

How to use proxy with authorization, through Selenium in java?

To set the proxy in the browser manually, I prescribe the address and port in the browser connection settings. After that, the first time you access any server in the browser, a dialog box pops up for authorization (basic authorization). After it passes, all requests go through the proxy.
--------------
In selenium I tried to do this:

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
    proxy.setHttpProxy("<login>:<password>@<address>:<port>");
    proxy.setSslProxy("<login>:<password>@<address>:<port>");
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability(CapabilityType.PROXY, proxy);
    WebDriver driver = new FirefoxDriver(capabilities);
    driver.get("http://2ip.ru");

In this case, in the browser's connection configuration, `` is set instead of the proxy address, and the default port ( zero ). If you add ` http://` , then `http` is inserted instead of the address, and the port is zero.
-------------
And yet, I tried this:
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
    proxy.setHttpProxy("<address>:<port>");
    proxy.setSslProxy("<address>:<port>");
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability(CapabilityType.PROXY, proxy);
    WebDriver driver = new FirefoxDriver(capabilities);
    driver.get("http://2ip.ru");

In this case, the address and port are written correctly and after starting the browser (on the line `WebDriver driver = new FirefoxDriver(capabilities);`) a dialog box appears with the text `Proxy "moz-proxy://:" requests a username and password. The site reports: "proxy"` and fields for entering login and password, the program execution is suspended, which makes it impossible to use the created `driver` object to switch to the window and enter the login and password (`driver.switchTo().alert(). ...`).
--------------
Also googled the option:
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
    proxy.setHttpProxy("<address>:<port>");
    proxy.setSslProxy("<address>:<port>");
    proxy.setSocksUsername("<login>");
    proxy.setSocksPassword("<password>");
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability(CapabilityType.PROXY, proxy);
    WebDriver driver = new FirefoxDriver(capabilities);
    driver.get("http://2ip.ru");

But the result is the same as in the second case.
-------------
Пробовал использовать еще такой вариант:
DesiredCapabilities capabilities = new DesiredCapabilities(); // DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("signon.autologin.proxy" , true);
profile.setPreference("network.websocket.enabled", false);
profile.setPreference("network.proxy.share_proxy_settings", false);
profile.setPreference("network.automatic-ntlm-auth.allow-proxies", false);
profile.setPreference("network.http.phishy-userpass-length", 255);
profile.setPreference("network.auth.use-sspi", false);
profile.setPreference("network.proxy.http", "<address>");
profile.setPreference("network.proxy.http_port", <port>);
profile.setPreference("network.proxy.ssl", "<address>");
profile.setPreference("network.proxy.ssl_port", <port>);
capabilities.setCapability(FirefoxDriver.PROFILE,  profile);
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setProxyType(ProxyType.MANUAL);
proxy.setSocksUsername("<login>");
proxy.setSocksPassword("<password>");
capabilities.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://2ip.ru");

If you set the proxy configuration through the Firefox profile ( profile.setPreference(...) ), then the window for authorization through the proxy appears a little earlier than the browser window (the browser starts up in about 1 second, and the window for authorization appears about half a second earlier) and when this, unlike the implementations from the previous message, does not block the main window (which is launched / displayed a little later). Also in this variant, program execution is not suspended at the driver creation line (`new FirefoxDriver(capabilities);`). The program pauses at the line `driver.get(" 2ip.ru");` , the browser waits for a response from 2ip.ru, until I manually enter the proxy authorization data, if authorization fails, then instead of the requested page I get a message - "The proxy server refuses to accept connections."
Question : how can I get access to the authorization window for further entering a login and password, or how to get authorized on a proxy server without displaying an authorization window (like I tried: proxy.setSocksUsername(""); proxy.setSocksPassword("" ); )?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Dubrovin, 2015-12-22
@z3apa3a

It was possible to install a local proxy without authorization, on which to register a proxy with authorization as a parent. In 3proxy
auth iponly
allow *
parent 1000 http ip_proxy port_proxy login password
proxy -i127.0.0.1 -p3128

C
CODER_ua, 2015-12-14
@CODER_ua

Solved via AutoItX4Java:

private void mozProxyAuth(String login, String password) {
        String JACOB_DLL_TO_USE = System.getProperty("sun.arch.data.model").contains("32") ? "jacob-1.18-x86.dll" : "jacob-1.18-x64.dll";
        File file = new File(System.getProperty("user.dir"), JACOB_DLL_TO_USE);
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
        AutoItX x = new AutoItX();
        if (x.winWait("Требуется аутентификация", null, 10)) {
            x.winActivate("Требуется аутентификация");
            x.send(login + "{TAB}" + password + "{ENTER}", false);
        }
    }

S
san_lex, 2016-03-25
@san_lex

To set a proxy, including authorization, it is enough to "feed" the following js script to the phantom:
phantom.setProxy('proxy address', 'port, 'http', ''login, 'password');
Works since phantomjs 2.0 version. In earlier - did not check.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question