Handling Alerts

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Handling Alerts

• Handle Alerts using the Alert Interface

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo {

public static void main(String args[]) throws InterruptedException {

WebDriver driver = new FirefoxDriver();

driver.get("https://iamsandesh23.github.io/selenium.github.io/");

driver.manage().window().maximize();

driver.findElement(By.id("alert1")).click();

Alert alert = driver.switchTo().alert();

String str = alert.getText();

System.out.println(str);

Thread.sleep(5000);

alert.accept();

driver.switchTo().defaultContent();

driver.close();

• Click on the button to display alert


• Switch to the displayed alert using switchTo().alert() of WebDriver
interface
• Read the text on the alert using getText() method of Alert interface
• Accept the alert using accept() method of Alert interface
• After accepting the alert, switch to the main window using
switchTo().defaultContent()
• Close the browser window

Handling Drop-down and Multi-selection box fields

• Select class is the predefined class in Selenium WebDriver and it has


predefined methods for performing various operations on Drop-down and
Multi-selection box fields
• Using selectByVisibleText() with a drop-down field to select any value
is a dropdown

package selectPack;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SelectDemo {

public static void main(String args[]) {

WebDriver driver = new FirefoxDriver();

driver.get("https://iamsandesh23.github.io/selenium.github.io/");

driver.manage().window().maximize();

WebElement dropdownField =
driver.findElement(By.id("drop1"));

Select select = new Select(dropdownField);

select.selectByVisibleText("doc 3");

}
• Using selectByVisibleText() with a Multi-selection box field to select
multiple values

package selectPack;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SelectDemo {

public static void main(String args[]) {

WebDriver driver = new FirefoxDriver();

driver.get("https://iamsandesh23.github.io/selenium.github.io/");

driver.manage().window().maximize();

WebElement multiSelectionBoxField =
driver.findElement(By.id("multiselect1"));

Select select = new Select(multiSelectionBoxField);

select.selectByVisibleText("Volvo");
select.selectByVisibleText("Swift");
select.selectByVisibleText("Audi");

• Using deselectByVisibleText() with a Multi-selection box field to


deselect multiple values

package selectPack;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SelectDemo {

public static void main(String args[]) {

WebDriver driver = new FirefoxDriver();

driver.get("https://iamsandesh23.github.io/selenium.github.io/");

driver.manage().window().maximize();

WebElement multiSelectionBoxField =
driver.findElement(By.id("multiselect1"));

Select select = new Select(multiSelectionBoxField);

select.selectByVisibleText("Volvo");
select.selectByVisibleText("Swift");
select.selectByVisibleText("Hyundai");
select.selectByVisibleText("Audi");

select.deselectByVisibleText("Swift");
select.deselectByVisibleText("Audi");

You might also like