How to Handle Alerts in Selenium?
There are the four methods that we would be using along with the Alert interface.
1. void dismiss()
The void dismiss method is used to click on the ‘Cancel’ button of the alert.
Java
driver.switchTo().alert().dismiss(); |
2. void accept()
The void accept method is used to click on the ‘OK’ button of the alert.
Java
driver.switchTo().alert().accept(); |
3. String getText()
The void accept method is used to capture the alert message..
Java
driver.switchTo().alert().getText(); |
4. void sendKeys(String stringToSend)
It is used to send some data to the prompt alert.
Java
driver.switchTo().alert().sendKeys( "Text" ); |
How to Handle Alert in Selenium using Java?
Imagine filling out a form online and accidentally missing some information. You only know if you made a mistake if the website tells you somehow, like with a pop-up message. This article explains what those pop-up messages are called in Selenium (alerts) and how to deal with them in your automated tests.