How To Handle Javascript Alerts/PopUps In Selenium WebDriver

Handling Javascript Alerts/PopUps:

In this post, we see how to handle javascript alerts/popus. Alerts are basically popup boxes that take your focus away from the current browser and forces you to read the alert message. You need to do some action such as accept or dismiss the alert box to resume your task on the browser.

To handle alerts popupswe need to do switch to the alert window and call Selenium WebDriver Alert API methods.

There are two types of alerts.

  1. Windows Based
  2. Web Based/Browser Based

Here in this post, I confine to Java Script Alerts (A.K.A. Browser/Web Based Alerts).

For Windows Based, Please check the below link.

How To Handle Windows Based Pop-ups Using AutoIT (WILL UPDATE SOON)

To handle Browser based Alerts (Web based alert popups), we use Alert Interface. The Alert Interface provides some methods to handle the popups.

While running the WebDriver script, the driver control will be on the browser even after the alert generated which means the driver control will be behind the alert pop up. In order to switch the control to alert pop up, we use the following command :

Once we switch the control from browser to the alert window. We can use the Alert Interface methods to do required actions such as accepting the alert, dismissing the alert, get the text from the alert window, writing some text on the alert window etc.,

Let’s see the Alert Interface Methods.

We need to Import a package org.openqa.selenium.Alert to handle the alerts in Selenium.

To get a handle to the open alert:

To Click on OK button:

To click on Cancel button.

To get the text which is present on the Alert.

To enter the text into the alert box

To Authenticate by passing the credentials

Sample Program:

If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.

Alerts

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

10 thoughts on “How To Handle Javascript Alerts/PopUps In Selenium WebDriver”

  1. How to handle Yes/No Pop up in selenium

    • Hi Vignesh, have you tried alert.accept and alert.dismiss

  2. Rajkumar nice blog

    • Thanks Dhananjay. Keep visiting.

  3. hi, mr.raj
    i want total tutorial pdf file

  4. how to accept alert after clicking through javascript executor as the above functionality not even executing after that

  5. Hi RajKumar,

    Your study materials are very useful for me, i am testing professional and i am learning selenium so i found your materials and your tutorials explains every topic with simple so it’s interesting to learn with practise. Thanks for providing such a wonderful tutorails.

    Regards,
    Subhash shekhar

    • I am glad that you found it useful Subhash Shekhar.

  6. YOUR VERY VERY THANKS A LOTS FROM MY END TO MAKE THIS WEBSITES I LEARN SO MANY BASIC THINGS. PLEASE KEEP WRITING. MAKE SOME CONTENT SPECIALLY ABOUT FRAME DESIGN ETC

    THanks

    • Thanks Punit. Sure we will try to write. Please share about this blog with your friends too.

Leave a Comment

Share via
Copy link
Powered by Social Snap