Is there any way to provide username and password to a basic auth dialog in IE using Selenium Webdriver? Passing the credentials in the URL is not an option for us.

- 36,924
- 42
- 155
- 176

- 12,272
- 32
- 97
- 202
-
Did anybody find a solution for this ? – Tejas Jun 10 '13 at 06:03
-
@Napster no, I'm using coded UI now because of this limitation – SB2055 Jun 10 '13 at 13:48
-
coded UI ? means you changed the server behavior to not to use basic authentication or something ? sorry, i didn't get it – Tejas Jun 12 '13 at 12:26
3 Answers
I got a solution for this epic problem
Use awt!!
Open the URL and use the java Robot class or the SmartRobot class given below:
class SmartRobot extends Robot {
public SmartRobot() throws AWTException
{
super();
}
/*public void pressEnter()
{
keyPress(KeyEvent.VK_ENTER);
delay(50);
keyRelease(KeyEvent.VK_ENTER);
} */
public void pasteClipboard()
{
keyPress(KeyEvent.VK_CONTROL);
keyPress(KeyEvent.VK_V);
delay(50);
keyRelease(KeyEvent.VK_V);
keyRelease(KeyEvent.VK_CONTROL);
}
public void type(String text)
{
writeToClipboard(text);
pasteClipboard();
}
private void writeToClipboard(String s)
{
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable transferable = new StringSelection(s);
clipboard.setContents(transferable, null);
}
}
and use this class something like....
try{
SmartRobot robot = new SmartRobot();
robot.type(username);
robot.keyPress(KeyEvent.VK_TAB);
robot.type(password);
robot.keyPress(KeyEvent.VK_ENTER);
}catch(Exception AWTException){
System.out.println("Exception " + AWTException.getMessage());
}
The solution works like a charm, without needing any 3rd party tools like AutoIt or Sikuli.

- 36,924
- 42
- 155
- 176

- 6,508
- 1
- 21
- 25
-
1This is similar to my method for handling basic auth in IE. However, I have found that IE < 9 I can pass the url in with user:pass@ using http authentication. With IE >= 9 I needed to pass in normal url and then use driver.switchTo().alert() in order begin using Robot for entering user/pass credentials. – Lukus Aug 02 '13 at 20:29
-
This method is not working in Firefox/Chrome/Safari..do you have any idea on this? This methods exactly mimics the human behavior. and I need to do the same with all browsers, sending parameters in url shows an error page from my SSO(shibboleth) because of lack of cookies. Only this solution with IE works like a charm ! – Tejas Aug 06 '13 at 06:44
Actually, if you are in Windows and using other than Java for writing the automation, AutoItX3 is a very good option.
You need to have registered AutoItX3.dll to Windows:
> regsvr32 AutoItX3.dll
And instantiate it somewhere in your code:
require 'win32ole'
@ai = ::WIN32OLE.new('AutoItX3.Control')
Here is Ruby/Watir-webdriver sample basic auth method:
def basic_auth(browser, user, pswd, url)
user_name, pass_word, login_button, login_title = get_basic_auth_control_indexes
a = Thread.new {
browser.goto(url)
}
if @ai.WinWait(login_title, "", 90) > 0
@ai.WinActivate(login_title)
@ai.ControlSend(login_title, '', "[CLASS:Edit; INSTANCE:#{user_name}]", '!u')
@ai.ControlSetText(login_title, '', "[CLASS:Edit; INSTANCE:#{user_name}]", @user)
@ai.ControlSetText(login_title, '', "[CLASS:Edit; INSTANCE:#{pass_word}]", @pass.gsub(/!/, '{!}'))
@ai.ControlClick(login_title, "", "[CLASS:Button; INSTANCE:#{login_button}]")
else
puts("Basic Auth Login window '#{login_title}' did not appear.")
end
a.join
end
Here are supporting methods: This one currently only knows Chrome for Win XP and Win 7
def get_basic_auth_control_indexes
case $win_major
when '5' # XP
['2','3','1','Connect to']
when '6' # Win 7
['1','2','2','Windows Security']
end
end
Of course this is Windows specific:
def get_windows_version
ver = `ver`.gsub("\n", '')
mtch = ver.match(/(.*)\s\[Version\s*(\d+)\.(\d+)\.(\d+)\]/)
$win_name = mtch[1]
$win_major = mtch[2]
$win_minor = mtch[3]
$win_build = mtch[4]
$win_version = "#{$win_major}.#{$win_minor}.#{$win_build}"
end

- 596
- 1
- 5
- 18
Did you try the good old passing username and password in URL?
driver.get("http://username:password@your-site.com");
It does work for me in Firefox and Chrome. I did not test the IE, because I am on linux

- 14,128
- 14
- 53
- 77
-
1This will not work in IE unless you change registry settings (which has its own caveats): http://stackoverflow.com/a/4105642/4985326 and http://stackoverflow.com/a/23519791/4985326 – Anon Feb 09 '17 at 16:00