小编典典

Webdriver-HTTP身份验证对话框

selenium

我有一个非常简单的selenium-webdriver脚本。我想使用webdriver进行HTTP身份验证。

脚本:

WebDriver driver = new FirefoxDriver();  
driver.get("http://www.httpwatch.com/httpgallery/authentication/");
driver.findElement(By.id("displayImage")).click();
Thread.sleep(2000);
driver.switchTo().alert().sendKeys("httpwatch");

问题:

driver.switchTo().alert().sendKeys("httpwatch");

抛出

org.openqa.selenium.NoAlertPresentException:不存在警报

题:

  • Webdriver是否仅找到警报对话框作为警报?
  • 在不使用AutoIt或http:// username:password @somesite的情况下,可以执行哪些自动化操作?

编辑

警报具有以下方法,但尚未实施。

driver.switchTo().alert().authenticateUsing(new UsernameAndPassword("username","password"))

阅读 601

收藏
2020-06-26

共1个答案

小编典典

问题在于 这不是javascript弹出窗口, 因此您无法通过selenium的对其进行操作alert()

如果AutoIt和URL中的提交凭据(最简单的选项-仅打开url,然后单击“显示图像”)都不适合您,则另一种方法是使用AutoAuthfirefox插件自动提交以前保存的凭据:

当您选择让浏览器保存登录信息时,AutoAuth会自动提交HTTP身份验证对话框。(如果您已经告诉浏览器您的用户名和密码,并且告诉它记住该用户名和密码,为什么不让它自动提交而不是每次都询问您呢?)

遵循Firefox中通过URL进行的HTTP BasicAuth中、建议的答案[、不起作用?、线:

  • 安装AutoAuth Firefox插件;
  • 访问需要身份验证的站点。输入您的用户名和密码,并确保选择保存凭据;
  • 将AutoAuth安装文件保存在硬盘上:在插件页面上,右键单击“添加到Firefox”和“将链接另存为”;
  • 实例化Firefox webdriver,如下所示:
FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
File pluginAutoAuth = new File("src/test/resources/autoauth-2.1-fx+fn.xpi");
firefoxProfile.addExtension(pluginAutoAuth);
driver = new FirefoxDriver(firefoxProfile);

此外,以类似于AutoIt选项的方式-
您可以使用sikuli屏幕识别和自动化工具在弹出窗口中提交凭据。


另请参阅其他想法和选项:

2020-06-26