C# OpenQA.Selenium.UnhandleAlertException:模式对话框显示错误消息

C# OpenQA.Selenium.UnhandleAlertException:模式对话框显示错误消息,c#,selenium,nunit,C#,Selenium,Nunit,我将附加代码以及应用程序的屏幕截图。我有一个提示窗口,其中需要输入文本。页面对象包含有关页面的所有必需信息,但脚本失败,因为它无法识别提示窗口 using System; using CSharpAutomationFramework.Framework.Base; using CSharpAutomationFramework.Framework.Core; using CSharpAutomationFramework.Framework.Helpers; using OpenQA.Sele

我将附加代码以及应用程序的屏幕截图。我有一个提示窗口,其中需要输入文本。页面对象包含有关页面的所有必需信息,但脚本失败,因为它无法识别提示窗口

using System;
using CSharpAutomationFramework.Framework.Base;
using CSharpAutomationFramework.Framework.Core;
using CSharpAutomationFramework.Framework.Helpers;
using OpenQA.Selenium;

namespace CSharpTestAutomation.PageObjects.Tcfa
{
    public class CashLoadingHo : BasePage
    {
        readonly String txtCarrierId = "id:=txtCLcarrierId";
        readonly String txtEnterCarrierId = "id:=txtCLcarrierId";
        readonly String btnCheckCarrierId = "id:=CmdCheckCarrierID";
        readonly String lstCashLoadType = "id:=ddlCLcashLoadType";
        readonly String lstCustomerAccountNumber = "id:=ddlCLcustAcId";
        readonly String lstPayment = "id:=ddlCLpayID";
        readonly String txtMerchantId = "id:=txtCLmerchantId";
        readonly String btnCheckMerchantId = "id:=CmdMerchantID";
        readonly String txtAmount = "id:=txtCLamt";
        readonly String txtEnterAmount = "id:=txtCLamt";
        readonly String txtNameofDepositor = "id:=txtCLdepositor";
        readonly String btnSave = "id:=CmdSave";
        readonly String btnPrintReceipt = "id:=cmdCLreceiptPrint";

        public CashLoadingHo(IWebDriver driver, Reporting reporter) : base(driver, reporter)
        {
            wrapper.SwitchToDefaultContent()
                   .SwitchToFrameWithName("main");
        }
        public CashLoadingHo CarrieridCheck(String carrierid)
        {
            wrapper.EnterText(txtCarrierId, carrierid)
                   .Click(btnCheckCarrierId);

            return this;

        }

        public CashLoadingHo CashLoadingDetails(String merchantid, String cashloadtype, String customeraccountnumber, String payment, string amount, string nameofthedepositor)

        {
            wrapper.SelectOptionFromList(lstCashLoadType, cashloadtype)
                   .SelectOptionFromList(lstCustomerAccountNumber, customeraccountnumber)
                   .SelectOptionFromList(lstPayment, payment)
                   .EnterText(txtMerchantId, merchantid)
                   .Click(btnCheckMerchantId)
                   .EnterText(txtAmount, amount)
                   .EnterText(txtNameofDepositor, nameofthedepositor);
            return this;
        }

        public CashLoadingHo ClickSave()
        {
            wrapper.Click(btnSave)
                   .AcceptAlert();
            return this;
        }

        public CashLoadingHo EnterCarrierId(string entercarrierid)
        {
            wrapper.EnterText(txtEnterCarrierId, entercarrierid);
            return this;
        }
    }
}


使用
driver.SwitchTo().alert().SendKeys(/*要发送到警报文本框的文本*/)切换到警报

然后用
driver.SwitchTo().Alert().accept()接受它

或者您可以从一开始就关闭它
driver.SwitchTo().Alert().disease()

您可以修改save函数来输入您的运营商ID,但我建议您不要这样做,因为即使您只想“保存”,也必须输入运营商ID。也许在某个时候,您可以单击“保存”,警报将不会出现

public CashLoadingHo ClickSave()
{
    wrapper.Click(btnSave)
           .AcceptAlert();
    return this;
}

public CashLoadingHo AlertEnterCarrierId(string id)
{
    WaitForAlert(); //Explained below
    driver.SwitchTo().Alert().SendKeys(id);
    driver.SwitchTo().Alert().Accept();
    //Now, swap back to your main window.
    driver.SwitchTo().DefaultContent();
    return this;
}

public CashLoadingHo AlertEnterAmount(string amount)
{
    WaitForAlert(); //Explained below
    driver.SwitchTo().Alert().SendKeys(amount);
    driver.SwitchTo().Alert().Accept();
    //Now, swap back to your main window.
    driver.SwitchTo().DefaultContent();
    return this;
}
然后,只需依次调用这3个函数。但是,根据我的经验,您应该等待警报出现,这样您就不会因为警报不存在而出现异常。创建另2个(处理警报的两个函数)将调用的等待函数

public CashLoadingHo WaitForAlert()
{
    //Initialize your wait object.
    WebDriverWait wait = new WebDriverWait(driver,TimeSpan.FromSeconds(10));
    wait.Until(ExpectedConditions.AlertIsPresent());
    return this;
}

很简单,首先切换到提示警报
driver.SwitchTo().Alert()
,然后使用
.sendKey()
传递值/文本,最后使用
.accept()
接受提示警报

请尝试下面的代码

//This step produce an alert on screen
IWebElement element = driver.FindElement(By.XPath("//*[@id='content']/p[11]/button"));

// 'IJavaScriptExecutor' is an 'interface' which is used to run the 'JavaScript code' into the webdriver (Browser)        
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", element);

// Switch the control of 'driver' to the Alert from main window
IAlert promptAlert = driver.SwitchTo().Alert();

// Get the Text of Alert
String alertText = promptAlert.Text;
Console.WriteLine("Alert text is " + alertText);

//'.SendKeys()' to enter the text in to the textbox of alert 
promptAlert.SendKeys("Accepting the alert");
Thread.Sleep(4000); //This sleep is not necessary, just for demonstration

// '.Accept()' is used to accept the alert '(click on the Ok button)'
promptAlert.Accept();

信用证:

当问问题时,请更具体一点:你期望什么?/你有什么错误?要获得帮助,请查看“”结果消息:OpenQA.Selenium.unhandleAlertException:Modal对话框存在我在运行脚本时收到了上面的错误消息。我还附上了在应用程序中保存给定数据后出现的提示窗口的屏幕截图。请将附加信息与您的问题联系起来。我已编辑了问题。给出的代码是屏幕截图的完整页面对象。您可以在屏幕截图的顶部看到一个提示窗口。提供所有必需信息并单击保存按钮后。此时会出现提示窗口,提示您输入其他信息以进行确认。我的代码在您输入必填信息并单击“保存”按钮之前运行良好。非常感谢您的解决方案。基本上,我是c#脚本的初学者。你能帮助我在代码中具体需要做哪些更改吗。如果您在我输入所有必填信息并单击“保存”后看到屏幕截图,则提示窗口将显示我要输入携带的id并在单击“确定”后单击“确定”,另一个提示窗口将显示在我需要输入金额的位置,然后再次单击“确定”。我的脚本只在单击“保存”按钮之前运行良好。请帮我做这个。真的非常感谢。这个解决方案非常完美。你的答案被接受了,谢谢你。你的解决办法很容易理解。在我的代码中,它也工作得非常好。我可以直接和你联系吗。关于我使用的框架,我没有更多的澄清。@Hargovind,如果我的解决方案是为你工作,那么首先接受我的回答。你可以参考如何回答。