internet-explorer - Selenium Webdriver 和 IE 中的基本身份验证

标签 internet-explorer selenium selenium-webdriver webdriver basic-authentication

有没有办法使用 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/

相关文章:

javascript - 向dom添加输入字段并在IE中聚焦

java - 如何仅使用列名从 Excel 中获取列值?

Selenium:指向默认 Chrome session

python - Selenium:查找存储在容器中的元素

java - 自定义 HTML 代码在 IE 9 中创建空列表

html - :active selector not applied on image links in ie

css - 如何在资源管理器中实现特定的字体样式?

python - 获取具有随机类名的元素

javascript - 在 Nightwatch 中设置 Firefox 首选项

selenium-webdriver - Selenium Java : I want to access first div element inside a div class