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.
- Confirm Box
- Alert Box
- Prompt Box
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.
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:
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(); } }
Command to click on Cancel button:
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.
Command to Click on OK button in alert box:
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:
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.
Command to enter text and Click on OK button:
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: