有没有办法使用 Selenium Webdriver 为 IE 中的基本身份验证对话框提供用户名和密码?在 URL 中传递凭据不是我们的选择。
问问题
5982 次
3 回答
3
我为这个史诗般的问题找到了解决方案
使用awt!!
打开 URL 并使用下面给出的 java Robot 类或 SmartRobot 类:
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);
}
}
并使用此类类似....
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());
}
该解决方案就像一个魅力,不需要任何第三方工具,如 AutoIt 或 Sikuli。
于 2013-06-21T11:40:19.070 回答
2
实际上,如果您在 Windows 中使用 Java 以外的语言编写自动化程序,AutoItX3 是一个非常好的选择。
您需要将 AutoItX3.dll 注册到 Windows:
> regsvr32 AutoItX3.dll
并在代码中的某处实例化它:
require 'win32ole'
@ai = ::WIN32OLE.new('AutoItX3.Control')
这是 Ruby/Watir-webdriver 示例基本身份验证方法:
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
以下是支持方法: 这个目前只知道 Chrome for Win XP 和 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
当然,这是特定于 Windows 的:
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
于 2013-08-15T21:33:25.447 回答
0
您是否尝试过在 URL 中使用旧的传递用户名和密码?
driver.get("http://username:password@your-site.com");
它在 Firefox 和 Chrome 中对我有用。我没有测试IE,因为我在linux上
于 2013-06-21T13:03:00.003 回答