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.‘.

 

 

 

 

 

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.‘.


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:

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:

Output:

Handle Prompt box in selenium


Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Handle cookies in Selenium WebDriver
Working with WebTables in Selenium
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *