Software Development and QA Tips By QASource Experts

How Do You Handle Alerts in Selenium?

Written by Dapheny Murphy | Jul 25, 2022 4:00:00 PM

Alerts are popup boxes that appear on the browser screen to provide information. It notifies errors, warnings, or seeks permission to perform a specific task. Selenium provides some default methods to handle alerts, as mentioned below:

  1. dismiss() // Clicks on ‘Cancel’ button
  2. accept() // Clicks on ‘Ok’ button
  3. getText() // Captures PopUp data
  4. sendKeys(String text) // Provides data in PopUp

Additionally, sometimes there are alert windows seeking permissions like microphone/camera access. These alerts can be handled by using your browser preferences. A sample Code snippet to handle such alerts is:

options.add_argument("--use-fake-ui-for-media-stream")
options.add_argument("--use-fake-device-for-media-stream")