Selenium Webdriver and Basic Auth in IE

2019-07-21 09:05发布

问题:

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.

回答1:

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.



回答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


回答3:

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