How to handle popup in selenium?
In popup handling we switch on alert box by using switchTo() with alert() on driver object.
Example:- Alert alert2 = driver.switchTo().alert();
and if you want to get alert text then use getText() on alert object.
Example: - String text2 =alert3.getText();
There are 3 type of alert in webpages.
1.simple alert.
2.confirmation alert
3.Text box alert
1.Simple alert box js code example:-
Here inbuilt alert method used for popup.
"function simpleAlert()
{
alert('ok')
}"
2.Confirmation alert js code example:-
Here inbuilt confirm method used.
"function confirmation(){
if (confirm(""Press a button!"")) {
txt = ""You pressed OK!"";
} else {
txt = ""You pressed Cancel!"";
}
}"
3.Text box alert js code example:-
In this we use prompt method of windows.
"
function myFunction() {
var txt;
var person = prompt(""Please enter your website:"", ""www.onlinetutorial.info"");
if (person == null || person == """") {
txt = ""User cancelled the prompt."";
} else {
txt = ""Hello "" + person + ""! How are you today?"";
}
document.getElementById(""demo"").innerHTML = txt;
}"
Example of popup handling in selenium with complete java code:-
"package pkg1;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyClass913_popup_handling {
public static void main(String[] args) {
System.setProperty(""webdriver.chrome.driver"",""C:\\Users\\Desk-105\\Documents\\SELENIUM\\chromedriver.exe"");
WebDriver driver = new ChromeDriver();
driver.get("https://www.onlinetutorial.info/p/selenium-popup-handling.html");
/**
* simple alert
*/
WebElement element = driver.findElement(By.id(""textbox_1""));
element.sendKeys(""pradeep yadav"");
WebElement element2 = driver.findElement(By.id(""button_submit""));
element2.click();
Alert alert = driver.switchTo().alert();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
alert.accept();
/**
* confirmation alert popup
*/
WebElement element3 = driver.findElement(By.id(""button_submit2""));
element3.click();
Alert alert2 = driver.switchTo().alert();
String text =alert2.getText();
System.out.println(text);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
alert2.accept();
/**
* text box popup
*/
WebElement element4 = driver.findElement(By.id(""button_submit3""));
element4.click();
Alert alert3 = driver.switchTo().alert();
String text2 =alert3.getText();
System.out.println(text2);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
alert3.accept();
// driver.close();
}
}
"
In popup handling we switch on alert box by using switchTo() with alert() on driver object.
Example:- Alert alert2 = driver.switchTo().alert();
and if you want to get alert text then use getText() on alert object.
Example: - String text2 =alert3.getText();
There are 3 type of alert in webpages.
1.simple alert.
2.confirmation alert
3.Text box alert
1.Simple alert box js code example:-
Here inbuilt alert method used for popup.
"function simpleAlert()
{
alert('ok')
}"
2.Confirmation alert js code example:-
Here inbuilt confirm method used.
"function confirmation(){
if (confirm(""Press a button!"")) {
txt = ""You pressed OK!"";
} else {
txt = ""You pressed Cancel!"";
}
}"
3.Text box alert js code example:-
In this we use prompt method of windows.
"
function myFunction() {
var txt;
var person = prompt(""Please enter your website:"", ""www.onlinetutorial.info"");
if (person == null || person == """") {
txt = ""User cancelled the prompt."";
} else {
txt = ""Hello "" + person + ""! How are you today?"";
}
document.getElementById(""demo"").innerHTML = txt;
}"
Example of popup handling in selenium with complete java code:-
"package pkg1;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyClass913_popup_handling {
public static void main(String[] args) {
System.setProperty(""webdriver.chrome.driver"",""C:\\Users\\Desk-105\\Documents\\SELENIUM\\chromedriver.exe"");
WebDriver driver = new ChromeDriver();
driver.get("https://www.onlinetutorial.info/p/selenium-popup-handling.html");
/**
* simple alert
*/
WebElement element = driver.findElement(By.id(""textbox_1""));
element.sendKeys(""pradeep yadav"");
WebElement element2 = driver.findElement(By.id(""button_submit""));
element2.click();
Alert alert = driver.switchTo().alert();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
alert.accept();
/**
* confirmation alert popup
*/
WebElement element3 = driver.findElement(By.id(""button_submit2""));
element3.click();
Alert alert2 = driver.switchTo().alert();
String text =alert2.getText();
System.out.println(text);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
alert2.accept();
/**
* text box popup
*/
WebElement element4 = driver.findElement(By.id(""button_submit3""));
element4.click();
Alert alert3 = driver.switchTo().alert();
String text2 =alert3.getText();
System.out.println(text2);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
alert3.accept();
// driver.close();
}
}
"