How To Handle Modals In Selenium


Welcome to this tutorial on learning how to work with different types of modal windows using Selenium WebDriver.

In this tutorial, we will cover the following topics.

Introduction

A modal is a popup window that is displayed on top of the current page. There are different types of modals; some can be very simple as the following.

Simple Modal Window

Others may contain forms to be filled out.

Modal Window With A Form

However, all modals require immediate attention, meaning that the user must either interact with the modal or dismiss it before returning to the main web page.

Entering Text In A Modal

Contrary to working with alert boxes, where we need to use driver.switchTo().alert(), there is no need to “switch to” a modal window in order to interact with it. All that is need in order to enter text in a modal window is to locate the element(s) that will contain the text and enter the text.

driver.findElement(locator).clear();
driver.findElement(locator).sendKeys(text);

The following video will show you how complete the form shown in the second modal shown earlier.

Conclusion

Modals are easy to work with and don’t require special methods to interact with them. When a user encounters a modal window, they must stop what they are doing and pay the modal immediate attention.

To practice this example for yourself, click here.

Leave a Reply