Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Handling Javascript Alerts/Popups In Selenium Webdriver

Posted on 03/21/2025 By admin

Hello Folks,

In last post, we have seen handing of website popups in selenium webdriver.

In this post, we will see how to handle JavaScript popups in selenium webdriver.

Types of JavaScript popups:

There are three types of popups in JavaScript which are given below:

  1. Alert popup
  2. Confirm popup
  3. Prompt popup

Alert popup:

  • It is a simple popup which shows some information to user in a small box within browser area.
  • It will have one “OK” button and close button.
  • Since, it is just a information box so OK button and close button will have similar functionality.
  • Popup will be disappeared when user clicks on either OK button or close button.
  • You can not inspect alert popup.
  • Without handling alert popup, you can not proceed further.

JavaScript code to create a alert popup:

Since you can not inspect alert popup, you can not handle it with selenium webdriver directly. So we need to use Alert interface.

Alert Interface:

Alert is an interface in selenium library. It has below methods in it:

  1. accept() : To click on OK button on popup.
  2. dismiss(): To click on close/cancel button of popup.
  3. getText(): To get text present on popup.
  4. sendKeys(java.lang.String keysToSend): To type on prompt popup.
  5. authenticateUsing(Credentials credentials) (Beta method)
  6. setCredentials(Credentials credentials) (Beta method)

A private class EventFiringAlert implements Alert interface.

Handling of alert popup in selenium webdriver:

To handle alert popup, first we need to switch to alert using switchTo() method. Syntax is as below:

Alert alert= driver.switchTo().alert();

  • alert() switches to the currently active popup for particular driver instance and return an Alert reference. It will throw NoAlertPresentException if no alert is present.

Java code:

Output: Alert text is:I am alert popup and you can not inspect me. You need to handle me using Alert interface. Alert closed successfully afetr accept.

Alert closed successfully after dismiss.

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 12: How To Run Inner TestNG Class From TestNG.xml ?
Next Post: Is It Possible To Use Page Factory Without FindBy In Selenium WebDriver?

Related Posts

June 10, 2018 – Make Selenium Easy Uncategorized
tooltip – Make Selenium Easy Uncategorized
How To Launch Firefox Browser in Selenium WebDriver – Java Uncategorized
August 25, 2018 – Make Selenium Easy Uncategorized
getAttribute() method in Selenium WebDriver – Why, What and How to use? Uncategorized
API Testing Tutorial Part 11 – Difference Between SOAP and Rest Web Services or API Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark