David Rett
David Rett

Reputation: 21

Close a Popup Dialog Box with Selenium Webdriver C#

I'm writing a test using C# for our website. When a record is deleted online, a popup dialog box appears that basically asks the user to confirm. Usually I can just inspect an element by right-clicking, but when this dialog box is up, nothing can be selected, in or out of the dialog box. When I tried using the IDE to see how it would handle the dialog popup, it gave this command:

Assert.IsTrue(Regex.IsMatch(CloseAlertAndGetItsText(), "^Are you sure you want to delete this batch[\\s\\S](\n|\r\n)All claims in this batch will be permanently deleted\\.(\n|\r\n)This action cannot be undone\\.$"));

That didn't work, so I also tried:

CloseAlertAndGetItsText();

But that didn't work either.
The box has two buttons, OK and cancel, and the OK button is already highlighted, so if there is a way to just do something like this:

driver.sendKeys(return);

But driver doesn't have a sendKeys command to call on its own.

Upvotes: 2

Views: 5800

Answers (1)

Darian Everett
Darian Everett

Reputation: 919

driver.SwitchTo().Alert().Accept();

That should be able to handle the type of dialog box you're describing. I've been able to use it for any dialog I've had to come across, so it works pretty well. I've also read about using IAlert but since I've never needed to use it, I have no idea how well it works, but if my solution doesn't work for you, maybe look into that.

Upvotes: 1

Related Questions