In this post, you will learn how to work with modals in Selenium WebDriver. There are two main types of modals. The first type is purely informative and does not require any user input. However, the second type of modal may require user input.
Let’s dive in
A modal is a popup window displayed on top of the current page, and there are different types of modals. Some can be very simple, as in the following example.
As you can see, this modal only displays a message and a button that a user can click on to close the modal.
You may need to fill out a form in other modals.
Notice that this modal not only contains a button to close the modal but that it also contains a form.
It is important to note that all modals require immediate attention. That means that a user must either interact with the modal or dismiss. Otherwise, they won’t be able to return to the main web page.
Modal vs alert boxes or windows
The main difference between a modal and an alert box is that modals do not require unique methods to work with them. For example, before interacting with an alert box, a user must switch to it.
Switching is not necessary for modals, as you will see next.
How to work with modals in Selenium
To enter text in a modal window you first locate the input element, then you simply enter the text. This code snippet shows you how to do this.
The following video will show how to complete the form shown in the second modal shown earlier.
And now you know how simple it is to handle modals in Selenium. You learned that modals do not require the use of unique WebDriver methods. When users encounter a modal window, they must stop what they are doing and pay the modal immediate attention.
To practice this example for yourself, go here.