Follow Tech Fairy in Twitter:-
As an Amazon Associate I earn from qualifying purchases.

How to accept alert in Selenium tests in Java & Python

Here’s How To Accept & Wait For Alerts In Selenium In Java & Java

Say you have created tests for all sorts of websites, but then one day, you were tasked with creating a test for a site that displays a Java Script alert? If you didn’t handle this alert, your test won’t work.

Accepting an alert is quite simple, and it only requires a few lines of code. You only need to use the dedicated function for those. You could either accept the alert right away, or to wait for it to appear first, since in some sites, the alert doesn’t appear right away.

The codes in this post are in both Python and Java, but this could be done in any language that Selenium supports.

Accepting The Alert

To accept the alert, all you need to do is to switch to the alert, then calling the “accept()” function. In the place in your test where the alert appears, use the following code:-

Java:-

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

Python:-

alert = driver.switch_to.alert
alert.accept()

If you need, you can ask the web driver to wait for the alert to present. Since in some sites, the alert may not appear right away. In that case, you could add this line to wait for the alert:-

Java:-

wait.until(ExpectedConditions.alertIsPresent());

Python:-

WebDriverWait(driver, 3).until(EC.alert_is_present(),'This message will be printed in the exception if the alert timed out')

Required Imports (in case you want to add them manually):-

Java:-

import org.openqa.selenium.Alert;

Python:-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

In case it has a cancel button, you can dismiss the alert if you like, since you may want to reject the alert in some of your tests:-

Java:-

alert().dismiss();

Python:-

alert.dismiss()



The Full Code

Here’s the full code that waits for the alert then accept it. You could edit the code to wait for the alert for more than I did, or to change the message that appears in case an exception was thrown:-

Java

public static void AcceptAlert(WebDriver driver) {
try {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (Exception e) {
// exception handling
e.printStackTrace();
}
}

Python:-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Add gecko driver to the path for this to work
driver = webdriver.Firefox()
driver.get("http://seleniumeasy.com/test/javascript-alert-box-demo.html")
button  = driver.find_element_by_class_name("btn-default")
button.click()
WebDriverWait(driver, 3).until(EC.alert_is_present(),
'This message will be printed in the exception if the alert timed out')
alert = driver.switch_to.alert
alert.accept()

And Finally

Accepting alert is very easy, and the code for doing so can be done by placing it in a function and calling it each time it appears in any of your tests.

As I mentioned earlier, accepting alerts can be done with the other languages supported by Selenium, like C# & Objective-C, and not just Java & Python.

I hope my post helped you learn to accept alert in all possible situation. If you have a suggestion on how to improve this guide, I am all ears.

See Also:-

Leave a Comment

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