Handle Alert and PopUp Using selenium:
What is Alert?
- Alert is a small message box that displays on-screen notifications to give the user some kind of information or asks for permission to perform some type of operation.
- It can also be used for warning purposes.
Type of Operation’s perform on Alert:
- accept() : To accept the alert.
- dismiss() : To dismiss the alert.
- getText() : To get the text of the alert.
- sendKeys() : To write some text to the alert.
Switch to Alert to perform some operation on Alert:
1 | Alert alert = driver.switchTo().alert(); |
There are 3 types of Alert box:
- Simple Alert :- It give the some information about the current screen or give some warning.
- Prompt Alert :- It Ask from the User to provide some inputs.
- Confirmation Alert :- This Alert box ask for a permission to perform some type of operation.
Package:
1 | import org.openqa.selenium.Alert; |
Alert Accept:
void accept() :- It allow programmer to click on the ‘OK’ Button of the Alert box.
1 | driver.switchTo().alert().accept(); |
Alert Dismiss:
void dismiss() :- It allow programmer to click on the ‘Cancel’ button of the Alert box.
1 | driver.switchTo().alert().dismiss(); |
Get Alert Text:
String getText() :- It simply allow programmer to capture the alert box.
1 | driver.switchTo().alert().getText(); |
Send Text To Alert:
void sendKeys(String stringToSend) :- It allow programmer to send data into alert box.
1 | driver.switchTo().alert().sendKeys(“Text”); |
Note: To Handle Window go to this Link.
Scenario’s:
- setProperty To Launch Browser.
- Open URL.
- Click on WebElement to Open Alert / PopUP.
- After click on WebElment create object of Alert to switch into Alert / PopUP.
- Perform Accept operation on WebElement.
- Perform Dismiss operation on WebElement.
- Perform GetText operation on WebElement.
- Perform sendKeys operation on WebElement.
Example of Handle Alert and PopUp Using selenium:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Alert_Popup_Handle { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Click on button to show Alert driver.findElement(By.id("Alert_ID")).click(); // Switch to Alert to perform some operation Alert alert = driver.switchTo().alert(); // Accept alert driver.switchTo().alert().accept(); // Dismiss alert driver.switchTo().alert().dismiss(); // GetText of alert driver.switchTo().alert().getText(); // SendKeys to particular alert driver.switchTo().alert().sendKeys("Sandeep"); } } |
Alert & Popup Window Handling in Selenium WebDriver and How to Handle Alerts/Popups in Selenium WebDriver