Tom
Tom

Reputation: 411

handling pop up with many buttons using selenium webdriver

I'm using selenium web driver with Java language. when there are two buttons in a pop up i.e. ok and cancel , it can be easily handled with web driver using the following code:

Alert alert = driver.switchTo().alert();
alert.accept(); // or alert.dismiss(); depending upon the action u want to perform.

but what to do when there are more than two button, i.e. there are 3 to 4 buttons in the pop up ( e.g. like ok,cancel,try again, ignore/continue), in that case what do we do? how can we click on whichever button that we want?

Thank you very much for your help in advance

Upvotes: 1

Views: 5682

Answers (2)

Petr Janeček
Petr Janeček

Reputation: 38424

What we're talking about are JavaScript's dialog popups. There's alert (has an OK button), confirm (has OK / Cancel) and prompt (has an input field and OK). Nothing more. Therefore, the dialog you're seeing is not a JavaScript dialog and can't be handled by Selenium's Alert interface.

You could be dealing with one of those two:

  1. A custom dialog like jQuery's dialog() (or something similar). That's good news! That's no real popup, that's just a well designed overlay consisting of normal HTML made to look like a dialog. You should be able to interact with this the usual way of WebDriver: inspect the elements with the tool of your choice, then find and click the button that needs to be clicked.
  2. A native browser's or even operating system's dialog (a download dialog, for example). That's bad news, as WebDriver can't handle these. Moreover, they tend to look differently across browsers / systems / language settings, so you'll need to detect and handle every case. Your options include:
    • The Robot class, it allows you to "press" programatically anything on the keyboard (or clicking blindly) and therefore getting rid of the dialog by, say, pressing Enter
    • AutoIt. It's a Windows program useful for handling any system-level automation.
    • That's more or less it. You can specify which dialog are you particularly dealing with and we might be able to come up with a better workaround. For example the download dialogs can be avoided entirely, etc.

Upvotes: 2

Matt
Matt

Reputation: 935

You only want to use alert() when dealing with native browser popup dialogs. If the web app your testing pops up an HTML dialog then you can select and click on any of the buttons using the element ID, xpath, CSS selector etc.

Upvotes: 0

Related Questions