"Automation is not about replacing jobs; it's about empowering people to do more meaningful work." — Unknown

4. Alerts and Popups

Estimated reading: 1 minute 31 views

In Selenium, Alerts and Popups are handled using the Alert interface, which provides several methods to interact with browser alerts. Here’s a comprehensive list of all the methods available for handling Alerts and Popups in Selenium.

Methods of Alert Interface in Selenium

  1. accept():
    This method is used to click the “OK” button on an alert or a confirmation dialog, or to accept a prompt.

  2. dismiss():
    This method is used to click the “Cancel” button on a confirmation alert (for confirmation or prompt dialogs).
  3. getText():
    This method retrieves the message text that is displayed in the alert dialog.
  4. sendKeys(String keysToSend):
    This method is used with prompt alerts (which allow user input) to send text into the input field of the alert.

Summary of Methods with Sample Syntax

Method NameDescriptionSample Syntax
accept()Accepts the alert (click “OK” or “Yes” button)alert.accept();
dismiss()Dismisses the alert (click “Cancel” or “No” button)alert.dismiss();
getText()Retrieves the text from the alertString alertText = alert.getText();
sendKeys(String)Sends text to a prompt alert (if the alert allows input)alert.sendKeys("Hello, Selenium!");

Leave a Comment

Share this Doc

4. Alerts and Popups

Or copy link

CONTENTS