How To Handle Javascript Alerts/PopUps In Selenium WebDriver
Handling Javascript Alerts/PopUps:
In this post, we see how to handle javascript alerts/popus. Alerts are basically popup boxes that take your focus away from the current browser and forces you to read the alert message. You need to do some action such as accept or dismiss the alert box to resume your task on the browser.
To handle alerts popupswe need to do switch to the alert window and call Selenium WebDriver Alert API methods.
There are two types of alerts.
- Windows Based
- Web Based/Browser Based
Here in this post, I confine to Java Script Alerts (A.K.A. Browser/Web Based Alerts).
For Windows Based, Please check the below link.
How To Handle Windows Based Pop-ups Using AutoIT (WILL UPDATE SOON)
To handle Browser based Alerts (Web based alert popups), we use Alert Interface. The Alert Interface provides some methods to handle the popups.
While running the WebDriver script, the driver control will be on the browser even after the alert generated which means the driver control will be behind the alert pop up. In order to switch the control to alert pop up, we use the following command :
driver.switchTo().alert();
Once we switch the control from browser to the alert window. We can use the Alert Interface methods to do required actions such as accepting the alert, dismissing the alert, get the text from the alert window, writing some text on the alert window etc.,
Let’s see the Alert Interface Methods.
We need to Import a package org.openqa.selenium.Alert to handle the alerts in Selenium.
To get a handle to the open alert:
Alert alert = driver.switchTo().alert();
To Click on OK button:
alert.accept();
To click on Cancel button.
alert.dismiss()
To get the text which is present on the Alert.
alert.getText();
To enter the text into the alert box
alert.sendkeys(String stringToSend);
To Authenticate by passing the credentials
alert.authenticateUsing(Credentials credentials)
Sample Program:
package softwareTestingMaterial; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class AlertInterface { @Test public void alertWindow() throws Exception{ System.setProperty("webdriver.chrome.driver", "D:\\Selenium Environment\\Drivers\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://softwaretestingplace.blogspot.com/2017/03/javascript-alert-test-page.html"); driver.findElement(By.xpath("//*[@id='content']/button")).click(); Thread.sleep(3000); Alert alert = driver.switchTo().alert(); String print = alert.getText(); System.out.println(print); alert.accept(); Thread.sleep(3000); driver.findElement(By.xpath("//*[@id='content']/button")).click(); Thread.sleep(3000); alert.dismiss(); driver.close(); } }
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.
How to handle Yes/No Pop up in selenium
Hi Vignesh, have you tried alert.accept and alert.dismiss
Rajkumar nice blog
Thanks Dhananjay. Keep visiting.
hi, mr.raj
i want total tutorial pdf file
how to accept alert after clicking through javascript executor as the above functionality not even executing after that
Hi RajKumar,
Your study materials are very useful for me, i am testing professional and i am learning selenium so i found your materials and your tutorials explains every topic with simple so it’s interesting to learn with practise. Thanks for providing such a wonderful tutorails.
Regards,
Subhash shekhar
I am glad that you found it useful Subhash Shekhar.
YOUR VERY VERY THANKS A LOTS FROM MY END TO MAKE THIS WEBSITES I LEARN SO MANY BASIC THINGS. PLEASE KEEP WRITING. MAKE SOME CONTENT SPECIALLY ABOUT FRAME DESIGN ETC
THanks
Thanks Punit. Sure we will try to write. Please share about this blog with your friends too.