OpenQA.Selenium.UnhandledAlertException:模式对话框显示错误消息

时间:2018-04-03 06:47:13

标签: c# selenium nunit

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

enter image description here

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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用driver.SwitchTo().Alert().SendKeys(/* The text to send to the alert textbox*/);

切换到您的提醒

然后只需使用driver.SwitchTo().Alert().Accept();

接受它

或者您可以从头开始关闭driver.SwitchTo().Alert().Dismiss();

您可以修改保存功能以输入您的运营商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;
}

答案 1 :(得分:-1)

简单,首先切换到提示警报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();

信用:ToolsQA