Benjamin02
Benjamin02

Reputation: 1

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

This is my code, when it gets to Switchto().Alert() there is an error saying 'No such 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

Upvotes: 0

Views: 480

Answers (1)

CEH
CEH

Reputation: 5909

You may need to wait for the alert to exist before clicking on it:

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();

If this still throws NoSuchAlertException, then the popup appearing may not truly be an alert, but possibly an HTML modal instead -- in which case, you can inspect it & use Selenium to find an appropriate selector to accept it.

Upvotes: 4

Related Questions