2

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.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
SB2055
  • 12,272
  • 32
  • 97
  • 202

3 Answers3

3

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.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Tejas
  • 6,508
  • 1
  • 21
  • 25
  • 1
    This 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
2

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
pmneve
  • 596
  • 1
  • 5
  • 18
0

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

Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
  • 1
    This 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