5

I'm writing selenium code in C# to interact with a form

This is the submit button:

<input type="submit" value="Submit" onclick="return confirm('Submit?');" class="SubmitButton">

This is my Selenium code to click the submit button.

IAlert alert = driver.SwitchTo().Alert();
alert.Accept();

Yet when I do, the 'ok' button is not clicked. Instead the dialogue box disappears and the form acts as if the submit input was never clicked. What am I doing wrong?

4 Answers 4

3

I don't know why your code was not working(my be version specific), Its working fine for me.

IAlert alert = driver.SwitchTo().Alert();
alert.Accept();

Any ways, you can do this way also,

SendKeys.SendWait("{ENTER}");

but before doing this make sure "System.Windows.Forms.dll" is added in your project References and also make sure your app is active when running, mean don't click on other window when popup appears and don't let your computer to be sleep.

Sign up to request clarification or add additional context in comments.

1 Comment

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 executenext line of code in c# which is weird.
0

It is ugly but... what I do I force the enter key for alerts and confirms

5 Comments

SendKeys.Send("{ENTER}"); just make sure the app is active when running, it should close the Alert window or confirm the Confirm javascript if this is the case... haters will hate, but we have a huge generic web tester application that works just like that...
I tried adding the alert.SendKeys("ENTER"); and it didn't work
which event are you calling SendKeys?
do a quick test, add SendKeys to a Tick event from Times and see this happening... after that you can figure where you prefer to add Sendkeys.
I don't know what you mean by event. I'm just using the driver to switch to an alert. IAlert alert = driver.SwitchTo().Alert(); and then alert.SendKeys("ENTER");. I don't know what it means to add sendkeys to a tick event.
0

If the dialog is disappearing from the screen. It means the Alert is getting Suppressed. You can use the Following Code:

DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();

ieCapabilities.setCapability("unexpectedAlertBehaviour", "ignore");

WebDriver driver = new InternetExplorerDriver(ieCapabilities);

1 Comment

This can be only used with remote web driver, and your answer confused me
0

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

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.