Handling Alerts using Selenium WebDriver

Handling alerts using Selenium WebDriver – The following are the Alert Interface provided by selenium. It is present in the org.openqa.selenium.Alert package. This Interface consists of the methods to deal with those alerts. They are as follows,

  • 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

The examples below will help you to learn about Handling alerts using Selenium WebDriver in detail.

Trending Now: Google’s Top Ranking site for SELENIUM 

Simple alert

Handling alerts using Selenium WebDriver

The first and the simple alert that we going to use in our Web Page is a simple alert, which display the OK button on them. It is used to read the text information from the page and displays simple information to the user.

Also here we also can able to switch the Alert message to the main window using driver.switchTo().alert().

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get(“http://toolsqa.wpengine.com/handling-alerts-using-selenium-webdriver/”);
driver.manage().window().maximize();

// This step will result in an alert on screen

driver.findElement(By.xpath(“//*[@id=’content’]/p[4]/button”)).click();
Alert simpleAlert = driver.switchTo().alert();
String alertText = simpleAlert.getText();
System.out.println(“Alert text is ” + alertText);
simpleAlert.accept();
}

Confirmation Alert

Handling alerts using Selenium WebDriver

Confirmation alert has two options “accept” and “dismiss”. The below code will show how to decline a prompt alert,

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get(“http://toolsqa.wpengine.com/handling-alerts-using-selenium-webdriver/”);
driver.manage().window().maximize();

// This step will result in an alert on screen

WebElement element = driver.findElement(By.xpath(“//*[@id=’content’]/p[11]/button”));
((JavascriptExecutor) driver).executeScript(“arguments[0].click()”, element);
Alert confirmationAlert = driver.switchTo().alert();
String alertText = confirmationAlert.getText();
System.out.println(“Alert text is “ + alertText);
confirmationAlert.dismiss();
}

Prompt Alerts

Handling alerts using Selenium WebDriver

Prompt alert used to get an information from the user. It displays the image in the alert box. Specially used when some input is required from the user. sendkeys()

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get(“http://toolsqa.wpengine.com/handling-alerts-using-selenium-webdriver/”);
driver.manage().window().maximize();

// This step will result in an alert on screen

WebElement element = driver.findElement(By.xpath(“//*[@id=’content’]/p[16]/button”));
((JavascriptExecutor) driver).executeScript(“arguments[0].click()”, element);
Alert promptAlert = driver.switchTo().alert();
String alertText = promptAlert .getText();
System.out.println(“Alert text is “ + alertText);

//Send some text to the alert

promptAlert .sendKeys(“Accepting the alert”);
Thread.sleep(4000;

//This sleep is not necessary, just for demonstration

promptAlert .accept();
}

Follow Our Free Selenium Tutorials Blog to be a Professional Selenium Tester.

Topics to Learn to be a Professional Selenium Tester