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

这是我的代码,到达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
maryk_ok 回答:如何使用C#和Selenium处理简单的Alert?

在单击警报之前,您可能需要等待该警报存在:

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查找合适的选择器以接受它。

本文链接:https://www.f2er.com/3017747.html

大家都在问