繁体   English   中英

如何使用 C# 和 Selenium 处理简单的警报?

[英]How do I handle a simple Alert using C# and Selenium?

这是我的代码,当它到达Switchto().Alert()时出现错误提示“没有此类警报”

            driver.FindElement(By.XPath("(.//*[normalize-space(text()) and normalize-space(.)='Add Document'])[1]/following::button[1]")).Click();

            **driver.SwitchTo().Alert().Accept();**

            var signFrame = driver.FindElement(By.Id("hsEmbeddedFrame"));
            driver.SwitchTo().Frame(signFrame);


            driver.FindElement(By.XPath("(.//*[normalize-space(text()) and normalize-space(.)='Istrong textnitials'])[1]/preceding::li[1]")).Click();strong text

您可能需要等待警报存在,然后才能单击它:

using OpenQA.Selenium.Support.UI;


WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));

// alert is present will automatically switch to the alert
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent());

// the below line is no longer necessary
// driver.SwitchTo().Alert().Accept();

如果这仍然抛出NoSuchAlertException ,那么出现的弹出窗口可能不是真正的警报,而是可能是 HTML 模式——在这种情况下,您可以检查它并使用 Selenium 找到合适的选择器来接受它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM