有没有办法使用 Selenium Webdriver 向 IE 中的基本身份验证对话框提供用户名和密码?我们无法选择在 URL 中传递凭据。
最佳答案
我找到了解决这个史诗问题的方法
使用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 等任何第三方工具。
关于internet-explorer - Selenium Webdriver 和 IE 中的基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16572956/