Just wanted to add that IAlert alert = driver.SwitchTo().Alert(); does not work at least in InternetExplorerWebDriver this might be because js cofirm alert once fired up blocks everything and you can't even execute next line of code in c# which is weird.
What I had to do is to actually execution of confirmation popup in a separate thread to release the control in c# backend so that the next line of code can be exectuted and also had to Thread.Sleep(1000) after and before sending keys to makes sure js is still not blocking
So my code looks something lie this:
public class MyThread
{
public IWebDriver driver;
public NgWebElement element;
public MyThread(IWebDriver _driver, NgWebElement _el)
{
driver = _driver;
element= _el;
}
public void RunMe()
{
AsyncJavaScriptExecutor asyncJavaScriptExecutor = new AsyncJavaScriptExecutor(driver as IJavaScriptExecutor);
asyncJavaScriptExecutor.ExecuteScript("arguments[0].click(); callback();", new object[] { element });
}
}
and then in the test or Page Object Model
MyThread mthread = new MyThread(_driver, element);
Thread oThread = new Thread(new ThreadStart(mthread.RunMe));
oThread.Start();
while (!oThread.IsAlive){}
//need to sleep beore and afer sending keys to makes sure js is not blicking events
Thread.Sleep(1000);
oThread.Abort();
SendKeys.SendWait("{ENTER}");
Thread.Sleep(1000);