Handling alerts and pop-ups
A pop-up is a browser window that opens randomly on surfing the Internet through the web browser. Web applications generate three different types of pop-ups, namely:
JavaScript alert (pop-ups) (for example, advertisements)
Browser pop-up (for example, a confirmation dialog box, an authentication prompt, and so on)
Native OS pop-ups (for example, Windows pop-ups such as upload/download notfications)
JavaScript pop-ups are generally in the form of alerts and advertisements, especially for marketing purposes. Selenium WebDriver provides an API to handle the JavaScript pop-ups. The following is an example of an alert:
Alert alert = driver.switchTo().alert();
Some of the helpful snippets using JavaScript alert functions are as follows:
The
dismiss()
function ignores or cancels the alert dialog box. The following is the syntax for this function:alert.dismiss();
Let's see an example to wait for the alert dialog box, which is expected to appear, and then skip it to move forward...