Documente Academic
Documente Profesional
Documente Cultură
(https://www.guru99.com/)
What is Alert?
How to handle Alert in Selenium WebDriver
How to handle Selenium Popup window using Webdriver
What is Alert?
Alert is a small message box which displays on-screen notification to give the user some
kind of information or ask for permission to perform certain kind of operation. It may be also
used for warning purpose.
1) Simple Alert
(/images/3-2016/032216_1314_AlertPopuph1.png)
2) Prompt Alert.
https://www.guru99.com/alert-popup-handling-selenium.html 1/14
This Prompt
12/13/2018 Alert asks some input Alert
from the user
& Popup Windowand selenium
Handling in Seleniumwebdriver
WebDriver can enter the text
using sendkeys(" input…. ").
(/images/3-2016/032216_1314_AlertPopuph2.png)
3) Confirmation Alert.
(/images/3-2016/032216_1314_AlertPopuph3.png)
https://www.guru99.com/alert-popup-handling-selenium.html 2/14
1) void
12/13/2018 dismiss() // To click on the Alert
'Cancel' button
& Popup Window of the
Handling alert.WebDriver
in Selenium
driver.switchTo().alert().dismiss();
driver.switchTo().alert().accept();
driver.switchTo().alert().getText();
driver.switchTo().alert().sendKeys("Text");
You can see a number of Alert methods are displayed as shown in below screen suggested
by Eclipse.
We can easily switch to alert from the main window by using Selenium's .switchTo()
method.
(/images/3-2016/032216_1314_AlertPopuph4.png)
In this scenario, we will use Guru99 demo site to illustrate Selenium Alert handling.
https://www.guru99.com/alert-popup-handling-selenium.html 3/14
Step
12/13/20181) Launch the web browser and
Alertopen
& Popupthe siteHandling in Selenium WebDriver
Window
"http://demo.guru99.com/test/delete_customer.php
(http://demo.guru99.com/test/delete_customer.php) "
(/images/3-
2016/032216_1314_AlertPopuph8.png)
Step 3) After entering the customer ID, Click on the "Submit" button.
(/images/3-
2016/032216_1314_AlertPopuph9.png)
https://www.guru99.com/alert-popup-handling-selenium.html 4/14
12/13/2018 Alert & Popup Window Handling in Selenium WebDriver
(/images/3-2016/032216_1314_AlertPopuph10.png)
https://www.guru99.com/alert-popup-handling-selenium.html 5/14
12/13/2018 Alert & Popup Window Handling in Selenium WebDriver
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.Alert;
driver.get("http://demo.guru99.com/test/delete_customer.php");
driver.findElement(By.name("cusid")).sendKeys("53920");
driver.findElement(By.name("submit")).submit();
// Switching to Alert
Alert alert = driver.switchTo().alert();
// Accepting alert
alert.accept();
}
Output :
When you execute the above code, it launches the site. Try to delete Customer ID by
handling confirmation alert that displays on the screen, and thereby deleting customer id
from the application.
https://www.guru99.com/alert-popup-handling-selenium.html 6/14
In automation,
12/13/2018 when we have multiple windows
Alert & in Handling
Popup Window any web application,
in Selenium WebDriver the activity may need
to switch control among several windows from one to other in order to complete the
operation. After completion of the operation, it has to return to the main window i.e. parent
window. We will see this further in the article with an example.
In selenium web driver there are methods through which we can handle multiple windows.
Driver.getWindowHandles();
To handle all opened windows by web driver, we can use "Driver.getWindowHandles()" and
then we can switch window from one window to another in a web application. Its return type
is Iterator<String>.
Driver.getWindowHandle();
When the site opens, we need to handle the main window by driver.getWindowHandle().
This will handle the current window that uniquely identifies it within this driver instance. Its
return type is String.
Now, we will automate the given below scenario to see how to handle multiple windows
using Selenium Webdriver.
In this scenario, we will use "Guru99" demo site to illustrate window handling.
Launch the browser and open the site " http://demo.guru99.com/popup.php "
(/images/3-
2016/032216_1314_AlertPopuph11.png)
When the user clicks on the " Click Here " link, new child window opens.
(/images/3-2016/032216_1314_AlertPopuph12.png)
https://www.guru99.com/alert-popup-handling-selenium.html 7/14
Step
12/13/2018 3) New Child Window opens. Alert & Popup Window Handling in Selenium WebDriver
A new window opens, ask the user to enter email id and submit the page.
(/images/3-2016/032216_1314_AlertPopuph13.png)
(/images/3-
2016/032216_1314_AlertPopuph14.png)
(/images/3-
2016/032216_1314_AlertPopuph15.png)
https://www.guru99.com/alert-popup-handling-selenium.html 8/14
When
12/13/2018 you execute the code, you will
Alertsee theWindow
& Popup childHandling
window is open
in Selenium in new tab.
WebDriver
(/images/3-
2016/032216_1314_AlertPopuph16.png)
(/images/3-2016/032216_1314_AlertPopuph17.png)
https://www.guru99.com/alert-popup-handling-selenium.html 9/14
12/13/2018 Alert & Popup Window Handling in Selenium WebDriver
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
driver.findElement(By.xpath("//*[contains(@href,'popup.php')]")).click();
String MainWindow=driver.getWindowHandle();
while(i1.hasNext())
{
String ChildWindow=i1.next();
if(!MainWindow.equalsIgnoreCase(ChildWindow))
{
driver.findElement(By.name("emailid"))
.sendKeys("gaurav.3n@gmail.com (mailto:gaurav.3n@gmail.com)");
driver.findElement(By.name("btnLogin")).click();
Output:
https://www.guru99.com/alert-popup-handling-selenium.html 10/14
When
12/13/2018you execute the above code,Alert
it launches theHandling
& Popup Window site and on clicking
in Selenium WebDriver the link "Click here,"
it opens up a child window in a new tab. You can close the child window, and switch to the
parent window once the operation is completely done. Hence handling more than one
window in the application.
(/images/3-2016/032216_1314_AlertPopuph18.png)
Conclusion:
We defined the types of alert and shown them with a screen shot.
Demonstrated handling the Alert with Selenium WebDriver using particular scenario.
Handled multiple windows with Selenium WebDriver using particular scenario.
Next (/handling-dynamic-selenium-webdriver.html)
Selenium Tutorials
1) Introduction (/introduction-to-selenium.html)
5) Locators (/locators-in-selenium-ide.html)
6) Enhancements (/enhancing-selenium-ide-script.html)
(https://www.facebook.com/guru99com/)
(https://twitter.com/guru99com)
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)
(https://forms.aweber.com/form/46/724807646.htm)
About
About US (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact US (/contact-us.html)
Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)
Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/)
https://www.guru99.com/alert-popup-handling-selenium.html 13/14
12/13/2018 Quiz (/tests.html)Alert & Popup Window Handling in Selenium WebDriver
Review (/best-ergonomic-mouse.html)
Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)
https://www.guru99.com/alert-popup-handling-selenium.html 14/14