How to click javascript confirm dialogue box with selenium webdriver in C# - Stack Overflow

I'm writing selenium code in C# to interact with a formThis is the submit button:<input type=&q

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?

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?

Share Improve this question asked Apr 10, 2012 at 17:34 jsmithjsmith 5753 gold badges14 silver badges29 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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 puter to be sleep.

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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744188592a4562324.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信