Skip to main content
 首页 » 编程设计

c#之数据重新发布后无法处理模态警报窗口(使用 Selenium webdriver C# 和 IE)

2024年01月01日44bonelee

我正在尝试在使用 Selenium WebDriver (selenium-dotnet-2.25.1) + IEDriverServer.exe (IEDriverServer_Win32_2.25.2) 重新发布数据后通过模态警报窗口。

这是我的测试 C# 代码:

    static void Main(string[] args) 
    { 
        IWebDriver driver = new OpenQA.Selenium.IE.InternetExplorerDriver(); 
 
        // Navigating to gmail.com and submitting wrong data 
        driver.Navigate().GoToUrl("gmail.com"); 
        driver.FindElement(By.Id("Email")).SendKeys("testuser"); 
        driver.FindElement(By.Id("signIn")).Click(); 
        Thread.Sleep(8000); 
 
        // making the data re-post by F5 
        driver.Navigate().Refresh(); 
 
        // Handling the aller and accepting it 
        IAlert alert = driver.SwitchTo().Alert(); 
        Console.WriteLine(alert.Text); 
        alert.Accept(); 
    } 

这是程序输出:

    Started InternetExplorerDriver server (32-bit) 
    2.25.2.0 
    Listening on port 2783 
    To display the webpage again, Internet Explorer needs to 
    resend the information you've previously submitted. 
 
    If you were making a purchase, you should click Cancel to 
    avoid a duplicate transaction. Otherwise, click Retry to display 
    the webpage again. 
    Press any key to continue . . .                  

结果,警告窗口消失,没有任何数据重新发布,并且 IE 显示“页面已过期”。

请指教。

请您参考如下方法:

“警报”在 Selenium WebDriver 中被定义为一件事,而且只有一件事:通过 JavaScript 显示的对话框。也就是说,Alert API 只能可靠地用于由 JavaScript alert()、confirm() 或 prompt() 创建的对话框功能。对于任何其他对话框,您可能会很幸运,但 API 并非设计用于处理这些对话框。