Handling Alert dialog, Popup windows and Frames | Handling Alert dialog

In the Previous Tutorial, we learned how to handle timeout issues in Selenium. In this tutorial, we will learn how to handle Alert windows.

What is an Alert window?

You must have noticed that on clicking some links/buttons, the page displays an alert window that asks the user to confirm if she or he wants to do this action by clicking on OK/Cancel or Yes/No buttons. Some of them display text like this – “Are you sure you want to perform this action?”. You can check it out yourself by going to the Automation Practice page. When you’ll click on the link – ‘Click Me to get Alert’, the page will prompt this alert:

This popup window is not part of the web page so normal Selenium code won’t be able to perform any action on it. It is a JavaScript Alert window. To handle these type of scenarios WebDriver has given Alerts interface.

Switch to Alert window

Launch the browser and navigate to the Automation Practice page.

System.setProperty("webdriver.chrome.driver", "C:\\teachmeselenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://cosmocode.io/automation-practice");
from selenium import webdriver

chrome_driver_path = "C:\teachmeselenium\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver_path)
driver.get("https://cosmocode.io/automation-practice")

Click on the link – Click Me to get Alert

driver.findElement(By.linkText("Click Me to get Alert")).click();
driver.find_element_by_link_text("Click Me to get Alert").click()

As this statement is executed, the browser will prompt an alert as displayed in the above screenshot. To perform any action on this alert window, WebDriver first, needs to switch to it.

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

You need to import package org.openqa.selenium.Alert for the Alert interface.

alert = driver.switch_to_alert()

Now we have an Alert object, we can perform actions on it.

Read the text of Alert window

String strAlertText = alert.getText();
alertText = alert.text;

Accept an Alert (click on OK/Yes) 

alert.accept();
alert.accept()

Dismiss an Alert (click on Cancel/No)

alert.dismiss();
alert.dismiss()

Looks like it has only three methods to remember?

When we type alert followed by a dot(.), your code editor will suggest some more options. Try it out and let me know your finding in comments.

Complete code

System.setProperty("webdriver.chrome.driver", "C:\\teachmeselenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://cosmocode.io/automation-practice");
driver.findElement(By.linkText("Click Me to get Alert")).click();
Alert alert = driver.switchTo().alert();
String strAlertText = alert.getText();
System.out.println(strAlertText);
alert.accept();
//alert.dismiss();
driver.quit();
from selenium import webdriver

chrome_driver_path = "C:\teachmeselenium\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver_path)
driver.get("https://cosmocode.io/automation-practice")

#Click on the link
driver.find_element_by_link_text("Click Me to get Alert").click()

#Switch to alert window
alert = driver.switch_to_alert()

#Get Text
print(alert.text)

#Accept alert
alert.accept()

#Reject alert
# alert.dismiss()

driver.quit()

This tutorial was so short and crispy. Wasn’t it? In the Next Tutorial, we will explore real-world challenges like handling popup windows.

16 thoughts on “Handling Alert dialog, Popup windows and Frames | Handling Alert dialog”

  1. Do we have something by which we can print in a dialog box while the script is executing.
    I mean in C# dot net we have MessageBox.show(" Script is at the middle");

    Do we have such thing in eclipse…. ?

    Also would like to know in this tutorial though we have used some drivers of selenium but GUI(front end) we have used only eclipse… Till now in most of the tutorials i have seen we have used eclipse and selenium just for finding elements.

    When we are going to use selenium GUI

    Regards
    Gaurav Khurana

    http://www.Udzial.com
    Udzial Means share

    Reply
  2. 1. That is not specific to eclipse. It is just an IDE over which you write java code. There are couple of ways in java to display message box. However it is not as simple as C# or Vbscript. Google "Java Message Box" and you will some ways…swing or JOptions

    2. You are getting confused buddy. The GUI that you are talking about is Selenium IDE, where we can record browser actions and play it back. We can also write some steps there. But these are specific to Selenium IDE. Selenium IDE, RC, WebDriver all these are different selenium variants. What we are following in tutorial is Selenium WebDriver that is the latest selenium variant. And eclipse is used as an IDE to code in Java. It is not specific to Selenium. If you were writing Selenium Webdriver code in C# you would have used Visual C#/Visual Studio IDE that are standard C# ediors.

    Reply
  3. Adding to previous comments… Selenium doesn't have its own editor to write code. you would need to depends on the editor specific to the language you are using as you know you can write selenium code in multiple languages… For Java you can use eclipse, netbeans etc… C#, Visual C#/Visual Studio…. Python, some other editros… You cannot write Selenium WebDriver or RC code in Selenium IDE. Its purely record-playback tool.

    Reply
  4. Ok got it. Can you cover Selenium IDE tool also a little bit as you will be knowing details for using the record playback tool. or some source where we can learn that too,
    I tried recording and playback and it worked great,, but would like to know the details further

    Regards
    Gaurav Khurana
    http://www.udzial.com
    Udzial Means Share

    Reply
  5. import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.JUnit4;
    import org.openqa.selenium.Alert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;

    @RunWith(JUnit4.class)
    public class Handling {

    @Test
    public static void main(String[] args) {

    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("http://www.cosmotechies.com/teachmeselenium/p/automation-practice.html");

    driver.findElement(By.linkText("Click Me to get Alert")).click();

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

    String strAlertText = alert.getText();
    System.out.println(strAlertText);
    alert.accept();

    driver.quit();
    }
    }

    Reply
  6. Hi Shadab,

    When I try to run the code as is, I get the following exception:

    Exception in thread “main” org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information)…

    Since I’m essentially getting a ‘not yet present’ error, do you suppose I should include a ‘waitTillElementIsDisplayed’ statement in the code and is this possible with an alert?

    Many thanks for this beautiful tutorials you have put together here. Much appreciated my friend!

    Reply
  7. I’m trying to build a automation test on the site https://pre.niio.com/singin
    when entering a wrong email, password and clicking login a popup appears. I’m trying the following code but I’m getting a “no such alert” exception.
    Alert alert = driver.switchTo().alert();
    alert.accept();
    why the popup is not recognize?
    pleas assist

    Reply
    • Hi Meir,

      The popup that you see on that page is not a JavaScript Alert. It is a regular page element part of the current page. If you right click over the OK button and click inspect you can see its propoerties. You can use the right properties to click that button.

      Reply

Leave a Reply