{"id":3142,"date":"2020-05-07T23:14:45","date_gmt":"2020-05-07T17:44:45","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3142"},"modified":"2025-04-02T10:29:17","modified_gmt":"2025-04-02T14:29:17","slug":"alert-and-popup-window-handling-in-selenium-webdriver","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/alert-and-popup-window-handling-in-selenium-webdriver\/","title":{"rendered":"Alert and Popup Window Handling in Selenium WebDriver"},"content":{"rendered":"\n<p>In this article, we will learn about the types of <strong>alerts<\/strong> found in web application testing and how to handle them using <strong>Selenium WebDriver<\/strong>. We will also explore how to <strong>accept<\/strong> and <strong>reject<\/strong> alerts based on their types. By understanding these concepts, you\u2019ll not only improve your testing skills but also enhance your readiness for <strong><a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium Certification<\/a><\/strong>, which will help you stand out in the competitive job market.<\/p>\n\n\n\n<p>In this article, you will learn<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>What is an Alert?<\/li>\n\n\n\n<li>How to handle Alert in Selenium WebDriver<\/li>\n\n\n\n<li>How to handle Selenium Popup window using Selenium WebDriver<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is Alert?<\/strong><\/h2>\n\n\n\n<p>Alert is a small message box that displays on-screen notification which provides the user some kind of information, If it takes User input then it\u2019s a popup message. It also is used for warning purposes. If convey or asks for permission it\u2019s a confirmation box. Alert is a popup window that displays on the screen. Users can perform many actions that can result in an alert on the screen. For example, when the user clicked on a button it displays a message or when you entered a form, the <a href=\"https:\/\/en.wikipedia.org\/wiki\/HTML\" rel=\"nofollow noopener\" target=\"_blank\">HTML<\/a> page asks for some extra information. Usually, alerts are different from regular windows. The main difference is that alerts are blocking in nature means that it will not allow users to perform any actions on the webpage if they are present.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Types of Alerts<\/strong><\/h2>\n\n\n\n<p>There are mainly three types of alerts<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Simple Alert: <\/strong>It displays some information and just has an OK button on them. It mainly displays some information to the user.&nbsp;<\/li>\n<\/ol>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;box type=\"shadow\"]document.alert(\u201cThis is a simple alert\u201d)<\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alert(\u201cThis is an alert\u201d);&#91;\/box]<\/code><\/pre>\n\n\n\n<p>2. <strong>Prompt Alert:<\/strong> In prompt alerts, you have an input box to add text to the alert box. This is used when the user is required to enter the input. We use <strong><em>sendKeys()<\/em><\/strong> method to type something in the Prompt alert box.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;box type=\"shadow\"]var person = prompt(\u201cDo you like testing?\u201d, \u201cYes\/No\u201d);&#91;\/box]<\/code><\/pre>\n\n\n\n<p><img fetchpriority=\"high\" decoding=\"async\" width=\"602\" height=\"386\" alt=\"prompt.PNG\" src=\"https:\/\/lh6.googleusercontent.com\/8BvQZFF-vlMP_gNUv1wZcgAmEKs66MsxQv_N6gQfU3sYICnUJ1oBPqJ2gx2ddgXBVzqsF-gT5kTApWxKhBQokyurTkVy7uk93Edd7rsFG0O9H6cpEk0OPt1KzRnLr9LjWSFYbgy4LHTxodmxaA\" title=\"\"><\/p>\n\n\n\n<p>3. <strong>Confirmation Alert: <\/strong>This alert has an option to accept or dismiss the alert. We use <strong><em>Alert.accept()<\/em><\/strong> to accept the alert and we use <strong><em>Alert.dismiss()<\/em><\/strong> to dismiss the alert.&nbsp;<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;box type=\"shadow\"]var confirmResult = confirm(\u201cConfirm pop up with OK and Cancel button\u201d);&#91;\/box]<\/code><\/pre>\n\n\n\n<p><img decoding=\"async\" width=\"591\" height=\"323\" alt=\"confirm.PNG\" src=\"https:\/\/lh5.googleusercontent.com\/E_7pXOqV3jEiUUMBxbwOOwPKzpi0Yzx7tY-e3VbBgGNTURxVLur56XiALBy2cNuiIBJRegiGei32ZbQVjkBiP0jJCml71YOikF3C9qLpOf3rD8xpeKjjFXbBNmP7o9gJbe74SZhFVXTYvEvMlQ\" title=\"\"><\/p>\n\n\n\n<p><strong>Alerts handling using Selenium WebDriver<\/strong><\/p>\n\n\n\n<p>Selenium provides an interface called <strong><em>Alert<\/em><\/strong> which is present in the <strong><em>org.openqa.selenium.Alert<\/em><\/strong> package. <a href=\"https:\/\/www.h2kinfosys.com\/blog\/difference-selenium-rc-webdriver\/\">Alert interface<\/a> has the following methods to deal with the alerts.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>accept():<\/strong> To accept the alert by clicking on the <strong>\u2018OK\u2019<\/strong> button of the alert.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/Ly4B9zIr_9GNkNsYM9Y5VrYF-5cHdpPvkY20zf5QbpTvounYaKUoeGdcyHJXV5FGzu02VHQqo1piL0qX2xUS6KQXKA2uQ3LuJPhLH-bwYM3RpiHUMkHJyiEOXacR7B8vmYCWrFUfjvcvX2_3NA\" alt=\"accept.PNG\" title=\"\"><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>dismiss():<\/strong> To cancel the alert by clicking on the <strong>\u2018Cancel\u2019<\/strong> button.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/O5hB1I2DC1Rq8jtB_OFZH_joMexg7ajQxbrXXysAj1QFQTrUwG8g9DVHjrPmEmTezjIHDOQtQRsl_ly4X_Mk6mOLoobn0U2YbBhNx6y55yKrq1PNvt9qnTZ1clz3TVFzqtXl85hYTTuHpbGfig\" alt=\"dismiss.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>getText():<\/strong> To get the text of the alert message.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/_9Jx-CACpIsPhoEk3JYMN6eCw1wNDyz6DsOYLBybBu-_JKrZHl9hX4-8Bz1xsIO1yhFS919Xn6sQji__VsqROI43lylCTdcIcUUulJgDph0blvP-1lvqtm10rXjFRWKnw6gdBtIr22JEDL4BBQ\" alt=\"text.PNG\" title=\"\"><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>sendKeys():<\/strong> To write text to the alert<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/B_D7RKp_Kvp3_fRo0b_APD1GnYeZNpDGMVPpaMkb7dv5Pmz9czESSDy1Uf77GwpcIQNiVR0yYQnfaYRKgu-AMOSnAYDZtblldAfgR5qMFtjn7xzKAWjfMwc_PHVZjrN69VObb9iSW5-w-LlrRg\" alt=\"keys.png\" title=\"\"><\/figure>\n\n\n\n<p>By using Selenium\u2019s <strong><em>.switchTo()<\/em><\/strong> method we can switch to the alert from the main window.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Working with the Alert box using Selenium Webdriver:<\/strong><\/h2>\n\n\n\n<p>Below is the sample code for alert box, make an HTML file and pass it to the webdriver.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/8bqLSH2QiwwYqmxjeDboiwaWlcj9WzSKW4uA5Vh-0u64EkU4FU37RRSyoTfwR7n_YZoH_nZQcN1YIHXYMkTaet6eqjeQboDGR0Zb3-2jfIE8NhJAlL7p958JKPX7Epsez3XDGTtGWyjKPf7C8A\" alt=\"alertexample.PNG\" title=\"\"><\/figure>\n\n\n\n<p>The below program will show Alerts popup using above html file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.openqa.selenium.Alert;\nimport org.openqa.selenium.By;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.chrome.ChromeDriver;\n\npublic class AlertPopupHandling {\n\n\tpublic static void main(String&#91;] args) throws InterruptedException {\n\t\t\/\/ TODO Auto-generated method stub\n\t\tSystem.setProperty(\"webdriver.chrome.driver\", \"D:\\\\Drivers\\\\geckodriver.exe\");\n\n        WebDriver driver = new ChromeDriver();\n\t\t\tdriver.manage().window().maximize();\n\t\t\tdriver.get(\"file:\/\/\/D:\/path\/alertpopup.html\");\n\t\t\tThread.sleep(2000);\n\t\t\tdriver.findElement(By.xpath(\"\/\/button&#91;@onclick='alertFunction()']\")).click();\n\t\t\tAlert alert=driver.switchTo().alert();\n\t\t\tSystem.out.println(alert.getText());\n\t\t\talert.accept();\n\n\n\t}\n\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Working with Confirmation Popup<\/strong><\/h2>\n\n\n\n<p>The below is the sample code for Confirmation Popup, make an HTML file and pass it to the webdriver.<\/p>\n\n\n\n<p><strong><img decoding=\"async\" alt=\"confirmation.PNG\" src=\"https:\/\/lh4.googleusercontent.com\/MFebU4ebPccY4fBPfRbLuFmWM08odapVCeiHQK28IfUojR6ue8cn_OHzcXoYoDznOvGJRTyGw_kzmqXZTBvKzqzYlW97IZPvO58GWo1xx-34uMzvM1wqMIMJkfA9sjFLlz1sN3X0LlvQ_yH4Ug\" width=\"602\" height=\"480\" title=\"\"><\/strong><\/p>\n\n\n\n<p>The below program will show Confirmation Popup using above html file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.openqa.selenium.Alert;\nimport org.openqa.selenium.By;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.chrome.ChromeDriver;\n\npublic class ConfirmPopupHandling {\n\n\tpublic static void main(String&#91;] args) throws InterruptedException {\n\t\t\/\/ TODO Auto-generated method stub\n\t\tSystem.setProperty(\"webdriver.chrome.driver\", \"D:\\\\Drivers\\\\geckodriver.exe\");\n\n        WebDriver driver = new ChromeDriver();\n\t\t\tdriver.manage().window().maximize();\n\t\t\tdriver.get(\"file:\/\/\/D:\/path\/confirmation.html\");\n\t\t\tThread.sleep(2000);\n\t\t\tdriver.findElement(By.xpath(\"\/\/button&#91;@onclick='confirmFunction()']\")).click();\n\t\t\tAlert alert=driver.switchTo().alert();\n\t\t\tSystem.out.println(alert.getText());\n\t\t\talert.dismiss();\n\n\t}\n\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Working with Prompt Popups<\/strong><\/h2>\n\n\n\n<p>Below is the sample code for Prompt Popup, make an HTML file and pass it to the webdriver.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"543\" height=\"456\" alt=\"prompthtml.PNG\" src=\"https:\/\/lh3.googleusercontent.com\/nEgVkn82bPbJTP58KQUGhE15x3Ix9GThJNI3VnwNqvQ8acZfU51cYcM5xeeIm2NKH65MzCil6jJsye3UOXFZD_h_8lgUFbLMMuSFP8GQb93offC50Nj2HVOCSMr2IUfdjikkgJ5yVodLkbCr7g\" title=\"\"><\/p>\n\n\n\n<p>The below program will show Prompt Popup using above html file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.openqa.selenium.Alert;\nimport org.openqa.selenium.By;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.chrome.ChromeDriver;\n\npublic class PromptPopupHandling {\n\n\tpublic static void main(String&#91;] args) throws InterruptedException {\n\t\t\/\/ TODO Auto-generated method stub\n\t\tSystem.setProperty(\"webdriver.chrome.driver\", \"D:\\\\Drivers\\\\geckodriver.exe\");\n\n        WebDriver driver = new ChromeDriver();\n\t\t\tdriver.manage().window().maximize();\n\t\t\tdriver.get(\"file:\/\/\/D:\/path\/prompt.html\");\n\t\t\tThread.sleep(2000);\n\t\t\tdriver.findElement(By.xpath(\"\/\/button&#91;@onclick='promptFunction()']\")).click();\n\t\t\tAlert alert=driver.switchTo().alert();\n\t\t\tdriver.switchTo().alert().sendKeys(\"Helllo\");\n\t\t\talert.accept();\n\t\t\tSystem.out.println(alert.getText());\n\n\n\t}\n\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Handling Selenium Pop-up window using Webdriver<\/strong><\/h2>\n\n\n\n<p>When we have multiple windows in any application, we may need to switch the control from one window to another window to complete the process. After completion of the process, it has to return to the main window i.e parent window.&nbsp;<\/p>\n\n\n\n<p>Methods to handle multiple windows in selenium webdriver.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Driver.getWindowHandles();<\/li>\n\n\n\n<li>Driver.getWindowHandle();<\/li>\n<\/ul>\n\n\n\n<p><strong>getWindowHandle():<\/strong> Gets the current window handle, which is opaque to this window that uniquely identifies it within this driver instance. It returns an id pointing to the window which is currently active. The return type is of <strong>String <\/strong>type. We use this as a reference to return to the same window after switching multiple windows.&nbsp;<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String mainWindow = driver.getWindowHandle();<\/code><\/pre>\n\n\n\n<p><strong>getWindowHandles(): <\/strong>This method returns a list of all the opened windows related to the specific driver. Its return type is a <strong>Set <\/strong>type<strong>.<\/strong>&nbsp; Because it eliminates the duplicate instances of the window with the same id if such duplicate instances are present.&nbsp;<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Set windowSet = driver.getWindowHandles();\n\nSystem.out.println(\"Total Windows Count: \"+windowSet.size());<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Programming approach to handling multiple windows<\/strong><\/h2>\n\n\n\n<p>We have two approaches to handle multiple child windows.<\/p>\n\n\n\n<p>Approach# 1: By using the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/control-statements-in-java\/\">For-Each loop<\/a><\/p>\n\n\n\n<p>Approach# 2: By using While loop with Iterator<\/p>\n\n\n\n<p><strong>Approach# 1: By using For-Each loop<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String defaultWindow = driver.getWindowHandle();\nSystem.out.println(\"Default window name is- \" +defaultWindow);\nThread.sleep(3000);\nSet&lt;String&gt; childWindows = driver.getWindowHandles();\nfor(String child : childWindows){\nif(!child.equalsIgnoreCase(defaultWindow)){\ndriver.switchTo().window(child);\nSystem.out.println(\"Child windows- \"+child);\n} else {\nSystem.out.println(\"There are no child windows\");\n}\n}<\/code><\/pre>\n\n\n\n<p><strong>Approach# 2: By using While loop with Iterator<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String defaultWindow = driver.getWindowHandle();\nSystem.out.println(\"Default window name - \" +defaultWindow);\nThread.sleep(3000);\nSet&lt;String&gt; childWindows = driver.getWindowHandles();\nIterator&lt;String&gt; iterator = childWindows.iterator();\nwhile(iterator.hasNext()){\nString child = iterator.next();\nif(!child.equalsIgnoreCase(defaultWindow)){\ndriver.switchTo().window(child);\nSystem.out.println(\"Child windows- \"+child);\n} else {\nSystem.out.println(\"There are no child windows\");\n}\n}<\/code><\/pre>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Iterator;\nimport java.util.Set;\nimport java.util.concurrent.TimeUnit;\n\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.chrome.ChromeDriver;\n\npublic class Popups {\n\n\tpublic static void main(String&#91;] args) throws InterruptedException {\n\t\t\/\/ TODO Auto-generated method stub\n\t\tSystem.setProperty(\"webdriver.chrome.driver\",\" D:\\\\Drivers\\\\geckodriver.exe \");\t\t\t\t\t\n        WebDriver driver = new ChromeDriver();\t\n        \n        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);\n        driver.get(\"https:\/\/www.naukri.com\/\");\n        driver.manage().window().maximize();\n        Thread.sleep(4000);\n        String defaultWindow = driver.getWindowHandle();\n        System.out.println(\"The Default window name - \" +defaultWindow);\n        Thread.sleep(3000);\n        Set&lt;String&gt; allchildWindows = driver.getWindowHandles();\n        Iterator&lt;String&gt; iterator = allchildWindows.iterator();\n        while(iterator.hasNext()){\n        String child = iterator.next();\n        if(!child.equalsIgnoreCase(defaultWindow)){\n        driver.switchTo().window(child);\n        System.out.println(\"Child windows- \"+child);\n        } else {\n        System.out.println(\"There are no child windows\");\n        }\n        }\n        driver.close();\n        driver.quit();\n        }\n        }\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn about the types of alerts found in web application testing and how to handle them using Selenium WebDriver. We will also explore how to accept and reject alerts based on their types. By understanding these concepts, you\u2019ll not only improve your testing skills but also enhance your readiness for [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":3155,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[719,722,720,575,721],"class_list":["post-3142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-alert-and-popup","tag-confirmation-popup","tag-handling-in-selenium","tag-types","tag-what-is-alert"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3142"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3142\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3155"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}