Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 569
jsmith Avatar asked Oct 24 '22 02:10

jsmith


1 Answers

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.

like image 56
Sopan Maiti Avatar answered Nov 01 '22 07:11

Sopan Maiti