9

How to override basic authentication in selenium2 chrome driver? I am facing an issue in my project where chrome "Authentication required" popup is coming which is blocking webdriver to continue navigation. Please find the attached screenshot for the same. enter image description here I am using following code to instantiate chrome driver,

private WebDriver driver;
@Override
protected void setUp() throws Exception {
    super.setUp();
    System.setProperty("webdriver.chrome.driver", "C:/Selenium/chromedriver.exe");
    driver = new ChromeDriver();
}
@Override
protected void tearDown() throws Exception {
    // TODO Auto-generated method stub
    super.tearDown();
}

Could you please help -

Thanks,

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
ABCDEFG
  • 789
  • 7
  • 13
  • 21
  • 1
    Please see [BASIC Authentication in Selenium 2 - set up for FirefoxDriver, ChromeDriver and IEdriver?](http://stackoverflow.com/q/5672407/1245497) – Martin Mar 27 '12 at 11:17
  • this is tell abt HTMLUnit framework and i tried to implement the same in my project. but code didnt work out, why b'se its not Selenium webdriver code. pls help with selenium webdriver code. thanks- – ABCDEFG Mar 28 '12 at 05:50

5 Answers5

8

I've struggled with the same problem over an hour and finally @kenorb's solution rescued me. To be short you need to add a browser extension that does the authentication for you (since Selenium itself can't do that!).

Here is how it works for Chrome and Python:

  1. Create a zip file proxy.zip containing two 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']
);

Don't forget to replace YOUR_PROXY_* to your settings.

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"
}
  1. Add the created proxy.zip as an extension

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_extension("proxy.zip")
    
    driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options)
    driver.get("http://google.com")
    driver.close()
    

That's it. For me that worked like a charm. If you need to create proxy.zip dynamically or need PHP example then go to the original post

Mike
  • 2,065
  • 25
  • 29
  • What about Java? How can I do that using java? – Ripon Al Wasim Mar 11 '16 at 10:44
  • Hi Mike, I tried to get your example working, but it failed. Would you mind having a look at my question? I have an example on Github https://stackoverflow.com/questions/44458165/err-tunnel-connection-failed-using-proxy-api-extension-with-selenium –  Jun 12 '17 at 14:36
  • I couldn't get this to work, but someone posted an answer in https://stackoverflow.com/questions/44458165/create-http-basic-auth-chrome-extension-for-selenium-mwe-available that did –  Jun 14 '17 at 13:34
  • Mike, I failed with this log: http://joxi.net/LmG3OVNTRbEMam The last lines are : `WebDriverException: Message: unknown error: cannot process extension #1 from unknown error: cannot read manifest (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.14393 x86_64)` Any advice? – Igor Savinkin Oct 03 '17 at 02:19
  • Ty soooo much! i was about to give up on this. Am using it with RobotFramework : ```python Set Proxy Extension ${chrome_options} = Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver # Call Method ${chrome_options} add_argument --incognito Call Method ${chrome_options} add_extension proxy.zip [return] ${chrome_options} ${chrome_options}= Set Proxy Extension Create Webdriver Chrome chrome_options=${chrome_options} ``` Does not work on incognito, see https://stackoverflow.com/a/17443982/665823 – Kalem Jan 02 '23 at 13:42
4

I think you're talking about using NTLM Authentication (windows integrated authentication) not Basic Authentication (where you provider your credentials in URL). Assuming that here is what you can try for running NTML auth in chrome:

Approach 1

Go to Internet Explorer, and open "Internet Options". Follow following steps:

  • Add your site to either Local intranet or Trusted sites add
  • List item
  • List item
  • List item
  • List item

After these changes, your chrome authentication should work. If you're wondering that how IE settings effect chrome behavior then, Chrome uses IE security settings for authentication.

Some good resources / credits:

  1. Good details
  2. Selenium issue details

Approach 2

Add your site to Local intranet and don't change anything for user authentication. By default, second option (Automatic logon only in Intranet sites) is selected.

Rohit
  • 6,365
  • 14
  • 59
  • 90
2

You can try adding the login credentials to the url get request (in Java):

driver.get("http://username:password@google.com/")
kenki
  • 538
  • 3
  • 4
  • 2
    Basic authentication doesn't have very good support in Selenium, as you've found. There is a feature request at the project page, and there are several comments and workarounds listed. You might want to check it out [here](http://code.google.com/p/selenium/issues/detail?id=34#c32) – kenki Mar 29 '12 at 15:55
  • Works for me with Selenium WebDriver – YoungDinosaur Oct 08 '13 at 15:40
  • I have written my code as above. But unfortunately, it's not working. – Ripon Al Wasim Mar 11 '16 at 10:43
0

I manage to do that sending the credentials twice. I don't know why, but in the second time the browser sends the packet, the authentication header goes with basic authentication.

My code (using C#):

string url = "http://user:password@google.com/";
IWebDriver webDriver = new ChromeDriver();
webDriver.Navigate().GoToUrl(url);
webDriver.Url = url;
Thiago
  • 1,103
  • 1
  • 16
  • 25
-1

Apart of configuring Network Proxy Preferences, you can set http_proxy in /etc/environment.

Other method is to use Chrome HTTP Private Proxy (which is based on Chrome-proxy-helper).

Here is PHP example (found in README):

$pluginForProxyLogin = '/tmp/a'.uniqid().'.zip';

$zip = new ZipArchive();
$res = $zip->open($pluginForProxyLogin, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFile('/path/to/Chrome-proxy-helper/manifest.json', 'manifest.json');
$background = file_get_contents('/path/to/Chrome-proxy-helper/background.js');
$background = str_replace(['%proxy_host', '%proxy_port', '%username', '%password'], ['5.39.64.181', '54991', 'd1g1m00d', '13de02d0e0z9'], $background);
$zip->addFromString('background.js', $background);
$zip->close();

putenv("webdriver.chrome.driver=/path/to/chromedriver");

$options = new ChromeOptions();
$options->addExtensions([$pluginForProxyLogin]);
$caps = DesiredCapabilities::chrome();
$caps->setCapability(ChromeOptions::CAPABILITY, $options);

$driver = ChromeDriver::start($caps);
$driver->get('https://old-linux.com/ip/');

header('Content-Type: image/png');
echo $driver->takeScreenshot();


unlink($pluginForProxyLogin);

The same logic should work for other languages as well.

More portable solution is reported already on SeleniumHQ GitHub.

See also:

Community
  • 1
  • 1
kenorb
  • 155,785
  • 88
  • 678
  • 743