Using Selenium WebDriver 2.53.1.1 in Visual Studio 2015 with C#.
My testing application has Alert Pop Ups that I successfully handle using the code snippet below:
ts.getDriver().SwitchTo().Alert().Accept();
Recently, I encountered a new challenge where some webelements were not being found anymore. To address this issue, I had to resort to running JavaScript to execute the web element as shown in the code snippet below:
public void SelectHREFCID()
{
IJavaScriptExecutor js = ts.getDriver() as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click()",hrefCID);
try
{
ts.getDriver().SwitchTo().Alert().Accept();
}
catch (NoAlertPresentException)
{
// do nothing...
}// This workaround helps find the hyperlink!
}
However, after executing the js.ExecuteScript line, an unexpected pop-up message appears, preventing my code from reaching the Alert().Accept() statement which then causes the program to halt at that point. Debugging revealed that I was unable to proceed beyond the js.ExecuteScript line. Any suggestions on how to manage this situation?
UPDATE: Upon executing the js.ExecuteScript command, https://i.sstatic.net/jM1m1.jpg
, the following dialog is displayed
https://i.sstatic.net/1MFnC.jpg
Once this pop up shows, my Selenium Code fails to continue into the try-catch block for handling the Alert
Latest update as of 9/9
I tried both ways of handling the alert https://i.sstatic.net/cnBPG.jpg https://i.sstatic.net/72iHx.jpg
However, my selenium code stops execution when the pop up appears
js.ExecuteScript("arguments[0].click().hrefCID);
**UPDATE 09/12****
I updated my window.alert and this resolved the issue (to confirm)
IJavaScriptExecutor js = ts.getDriver() as IJavaScriptExecutor;
js.ExecuteScript("window.confirm = function(){return true;}");
js.ExecuteScript("arguments[0].click()",hrefCID);
// js.ExecuteScript("window.alert = function() { return true;}");