如何取消身份验证登录弹出窗口

时间:2019-08-06 15:54:59

标签: c# selenium selenium-webdriver selenium-chromedriver

当我打开页面https://the-internet.herokuapp.com/basic_auth时,我将显示身份验证登录弹出窗口。如何单击“取消”?

我试图解除此警报,但驱动程序停止了以下代码:

driver.Url = "https://the-internet.herokuapp.com/basic_auth"

我得到一个错误。

这是我的代码:

    {
        [TestMethod]
        public void TestMethod1()
        {
            IWebDriver driver = new ChromeDriver();

            driver.Url = "https://the-internet.herokuapp.com/basic_auth";

            IAlert alert = driver.SwitchTo().Alert();
            alert.Dismiss();

        }
    }

我在Visual Studio中收到一条错误消息:

  

“ OpenQA.Selenium.WebDriverException:对URL http://localhost:51508/session/5c55323e7618568db8a156ea224f181e/url的远程WebDriver服务器的HTTP请求在60秒后超时。”

2 个答案:

答案 0 :(得分:0)

与可以交互的JavaScript警报不同,W3C标准不支持与诸如基本身份验证之类的浏览器窗口进行交互。

提示将阻止Web驱动程序运行,锁定其接收命令的功能并阻止其正确关闭,从而要求在运行后或清理过程中将其终止。

如果必须找到一种处理方法,则需要使用第三方库,例如InputSumulator。这是Net Core版本的链接:https://www.nuget.org/packages/InputSimulatorCore/

答案 1 :(得分:0)

搜索nuget软件包AutoIT并安装它。

在您的代码中添加using语句:

using AutoIt;

然后,您需要根据“窗口标题”添加类似以下内容的内容。如果您转到https://www.autoitscript.com/site/autoit/downloads/,则可以在本地下载它,这里有一个检查器工具,您可以在其中查看标题,类等弹出信息。

if (AutoItX.WinExists("https://the-internet.herokuapp.com/basic_auth - Google Chrome") ==  1) //if it displays
{
var winhandle = AutoItX.WinGetHandle("https://the-internet.herokuapp.com/basic_auth - Google Chrome"); //get the window handle
AutoItX.WinKill(winhandle); //close popup
}