3

Selenium WebDriver 2.53.1.1 Visual Studio 2015 C#

The application I am testing has Alert Pop Ups which I handle with the following bit of code without problem

 ts.getDriver().SwitchTo().Alert().Accept();

However I have ran into a new issue. Some of my webelements have issues of not being found anymore and therefore I have to run JavaScript to execute the web element (please see code below for handling the web element)

    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...
        }// Have to use this to find the hyperlink!
    }

The only problem is immediately after js.ExecuteScript line runs, the Pop Up Displays and my Alert().Accept() line of code never fires off after this so the program just stops there with the pop up. I ran this line by line in debug and can't can't step to the next line once I execute the js.ExecuteScript line. Any advice on how to handle this?

UPdate At this point in my code (js.ExecuteScript)) enter image description here

, as soon as this line of code is executed i see this

enter image description here

Once this pop up displays My Selenium Code does not continue into the try catch statement to handle the Alert

Latest update as of 9/9

I tried the alert handle both ways enter image description here enter image description here

But my selenium code stops execution at the point of the pop up firing off

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;}");
10
  • Not clear what you're asking?? What do you mean program just stops there with the pop up?? Is there any exception or something else?? Commented Sep 8, 2016 at 19:00
  • Sorry for any confusion. Basically if I used the Alert().Accept() while calling Web.Element.Click it would execute this line of code. (I know this for sure b/c some of my webelement that are working fine when a pop up displays, however because I am executing the web element action through javascript it never tries to execute the alert pop up after. I hope this makes more sense. Commented Sep 8, 2016 at 19:15
  • You mean JavaScript click does not open popup. Right?? Commented Sep 8, 2016 at 19:18
  • No the pop up does open as soon as the javascript code is ran but my Selenium Code does not continue at this point Commented Sep 8, 2016 at 19:49
  • 1
    updated my window alert statement above this resolved my issue. Thank you @SaruabhGaur If you will reply with the answer Ill give you credit! Thank you again! Commented Sep 12, 2016 at 14:32

1 Answer 1

1

I think this alert blocks the code execution, you should execute this script before click to override the confirmBox function using js as :-

js.ExecuteScript("window.confirm = function() { return true;}")

After that execute as below to perform click on button :-

js.ExecuteScript("arguments[0].click()",hrefCID);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.