Alerts are basically popup box/window that takes the focus away from the current browser screen and forces you to read the alert message and do some action.
Once you take any action (accept or dismiss), it allows to resume performing the task on browser.
There are basically 3 types of alertsAlert box
, Confirm box
, and Prompt box
.
We will see how to handle each of these diff types of alerts in selenium
A simple alert:
in this either you can accept or dismiss an alert
package controls; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Alerts1 { static WebDriver driver; public static void main(String[] args) { driver=new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://qavbox.github.io/demo/alerts/"); driver.findElement(By.name("commit")).click(); if(IsAlertPresent()==1) //simply written a logic to chk if alert present { Alert alert=driver.switchTo().alert(); alert.accept(); } } public static int IsAlertPresent() { WebDriverWait wait = new WebDriverWait(driver, 300 /*timeout in seconds*/); if(wait.until(ExpectedConditions.alertIsPresent())==null) return 0; else return 1; } }
Alert exception handling
We can handle the alert exception (org.openqa.selenium.NoAlertPresentException), this exception appears when we try to switch to an alert, but alert not found.
Let’s make a failed scenario, where we are trying to switch to alert but alert not appearing.
Note –
If you are using webDriverWait for an alert, then TimeoutException appears if time out.
Here is the detail of code implementation for both the exceptions to handle an alert.
public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/Users/sunilkumarpatro/sel/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("https://qavbox.github.io/demo/alerts/"); driver.findElement(By.name("commit")).click(); try { new WebDriverWait(driver, 5) .until(ExpectedConditions.alertIsPresent()); Alert alert=driver.switchTo().alert(); alert.accept(); }//catch(org.openqa.selenium.NoAlertPresentException e) catch(org.openqa.selenium.TimeoutException e) { System.out.println("--------------No Alert found---------------"); System.out.println( "Reason :- " + e.getMessage()); } driver.quit(); }
Prompt alert
Here we can enter text in alert textbox, check the checkboxes and can accept / dismiss…
public static void main(String[] args) throws AWTException { driver=new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://qavbox.github.io/demo/alerts/"); driver.findElement(By.id("prompt")).click(); if(IsAlertPresent()==1) //simply written a logic to chk if alert present { Alert alert=driver.switchTo().alert(); alert.sendKeys("Kumar"); alert.accept(); } }
If you want to handle the “prevent this page…” mark box on the dialog, then we can use Robot class in java to send key events to the alert like TAB, SPACE BAR to check the checkbox.
Note : This below code even explains, the usage of Robot java class in selenium.
if(IsAlertPresent()==1) { Alert alert=driver.switchTo().alert(); alert.sendKeys("Kumar"); Robot r = new Robot(); r.keyPress(KeyEvent.VK_TAB); Thread.sleep(2000); r.keyPress(KeyEvent.VK_SPACE); Thread.sleep(2000); alert.accept(); }
Username and password alert
Some browser alerts like the above screenshot, to enter username and password, doesn’t reflect on html dom and hence can’t be identified.
in this case we can pass the username and password while launching the browser,
Simple solution is to pass username and password while launching browser.
String siteurl = "xyz.com" String URL = "http://" + "<UserName>" + ":" + "Password" + "@" + siteurl; driver.get(URL);
Confirm alert
Either you can accept and dismiss the alert.
if u click on “prevent this from…” checkbox, you won’t get the alert again for that browser instance…
public static void main(String[] args) throws AWTException, InterruptedException { driver=new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://qavbox.github.io/demo/alerts/"); driver.findElement(By.id("confirm")).click(); if(IsAlertPresent()==1) //simply written a logic to chk if alert present { Alert alert=driver.switchTo().alert(); alert.accept(); //for cancel //alert.dismiss(); } }