Handling different type of pop ups in Selenium WebDriver

In the previous post , we have seen how to handle the cookies of browsers in Selenium. In this post, we will learn handling different type of pop ups in Selenium WebDriver

For demo purpose, we have created a Demo Application for different pop-ups. There are different type of boxes.

  1. Confirm Box
  2. Alert Box
  3. Prompt Box
Pop-ups handling in selenium

Fig 1.0


1. Confirm Box: 

It is a Pop-up with 2 buttons named as Cancel and OK. We can get this pop-up by clicking on the button ‘Show confirm box’ as shown in above figure 1.0.

confirm box in selenium

 

There can be 2 functionalities , either we can click on OK button or Cancel button. Let’s see how to handle this in selenium.

Command to click on OK button:

driver.switchTo().alert().accept();

After clicking on the OK button, it returns the massage ‘Confirmed.‘.

public class PopUpExample {
		private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
			
			driver = new FirefoxDriver();
			
			driver.manage().window().maximize();
			driver.get("https://testingpool.com/wp-content/uploads/2015/08/AlertHandles.html");
			//Click on the confirm box
			WebElement ConfirmBox = driver.findElement(By.xpath("//*[@id='btnConfirm']"));
			ConfirmBox.click();
			
			// Click on OK button
			driver.switchTo().alert().accept();
	 }
}

 

 

 

 

 

Confirm box in selenium

 

Command to click on Cancel button:

driver.switchTo().alert().dismiss();

After clicking on the OK button, it returns the massage ‘Rejected.‘.

public class PopUpExample {
		private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
			
			driver = new FirefoxDriver();
			
			driver.manage().window().maximize();
			driver.get("https://testingpool.com/wp-content/uploads/2015/08/AlertHandles.html");
			//Click on the confirm box
			WebElement ConfirmBox = driver.findElement(By.xpath("//*[@id='btnConfirm']"));
			ConfirmBox.click();
			
			// Click on Cancel button
			driver.switchTo().alert().dismiss();
	 }
}

2. Show Alert: 

As shown in figure 1.0, If you click on the button ‘Show alert’. It displays a pop up with a OK button.

Alert box with selenium

Command to Click on OK button in alert box:

driver.switchTo().alert().accept();

After clicking on the OK button, it returns the massage Alert is gone.”.

Code for clicking OK button:

public class PopUpExample {
		private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
			
			driver = new FirefoxDriver();
			
			driver.manage().window().maximize();
			driver.get("https://testingpool.com/wp-content/uploads/2015/08/AlertHandles.html");
			//Click on the Alert  box
			WebElement AlertBox = driver.findElement(By.xpath("//*[@id='btnAlert']"));
			AlertBox.click();
			
			// Click on OK button
			driver.switchTo().alert().accept();
	 }
}

Output:

Alert box handling in selenium


3. Prompt Box:

As shown in figure 1.0, If you click on the button ‘Show Prompt’. It displays a pop up with a edit box along with OK and cancel button.

You can enter the desired text under the Edit box and click OK. The same text will be displayed at the page.

Prompt box

Command to enter text and Click on OK button:

driver.switchTo().alert().sendKeys(“Learning Selenium”);
driver.switchTo().alert().accept();

Code to work with Prompt box:

public class PopUpExample {
		private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
			
			driver = new FirefoxDriver();
			
			driver.manage().window().maximize();
			driver.get("https://testingpool.com/wp-content/uploads/2015/08/AlertHandles.html");
			//Click on the Alert  box
			WebElement PromptBox = driver.findElement(By.xpath("//*[@id='btnPrompt']"));
			PromptBox.click();
			
			// Enter data
			driver.switchTo().alert().sendKeys("Learning Selenium");
			driver.switchTo().alert().accept();
	 }
}

Output:

Handle Prompt box in selenium


Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...