What is NoAlertPresentException in Selenium?

What is an exception?

An exception is an event that occurs during the execution of a program that disrupts the normal flow of the program’s instructions.

Why does NoAlertPresentException occur in Selenium?

Generally, the Selenium program driver tries to switch to an alert box or pop-up that is displayed on top of the webpage when an exception occurs.

However, if there is no alert box present at the time of switching, then Selenium raises NoAlertPresentException instead.

selenium.common.exceptions.NoAlertPresentException: Message: no such alert

In some cases, this may happen because the website you want to automate may not have loaded fully. It’s also possible that the alert box element is not loaded into DOM yet.

Demo

In this demo, we go through how NoAlertPresentException raises and how to resolve it.

  • When we try to run the code below, it raises NoAlertPresentException.
  • This is because the code expects an alert button to be clicked, which it will eventually switch to.
  • However, in the following code, we try to switch to the alert box without clicking the button.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH=r"C:\Users\GUTKRISH\chromedriver.exe"
#specify where your chrome driver present in your pc
driver = webdriver.Chrome(PATH)
#provide website url here
driver.get("https://omayo.blogspot.com/")
time.sleep(2)
#finds element by id
alert_button = driver.find_element_by_id("alert1")
#click the button
#alert_button.click()
#It will raise exception as we are not clicking the button
#switch to alert and click ok button
driver.switch_to.alert.accept()

Resolve the exception

To resolve this, we need to click the button and switch to the alert box.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH=r"C:\Users\GUTKRISH\chromedriver.exe"
#specify where your chrome driver present in your pc
driver = webdriver.Chrome(PATH)
#provide website url here
driver.get("https://omayo.blogspot.com/")
time.sleep(2)
#finds element by id
alert_button = driver.find_element_by_id("alert1")
#click the button
alert_button.click()
#switch to alert and click ok button
driver.switch_to.alert.accept()

It is possible that the element is not loaded into the DOM yet, so you can wait until that element loads.

WebDriverWait(driver, 10).until(EC.alert_is_present())

Consequently, the code becomes:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH=r"C:\Users\GUTKRISH\chromedriver.exe"
#specify where your chrome driver present in your pc
driver = webdriver.Chrome(PATH)
#provide website url here
driver.get("https://omayo.blogspot.com/")
time.sleep(2)
#finds element by id
alert_button = driver.find_element_by_id("alert1")
#click the button
alert_button.click()
# It will wait 10 seconds till alert loads into DOM
WebDriverWait(driver, 10).until(EC.alert_is_present())
#switch to alert and click ok button
driver.switch_to.alert.accept()

Handling exceptions

The script will terminate when an exception is raised. We can handle exceptions with the try except block and continue with the flow of the script.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.common.exceptions import NoAlertPresentException
PATH=r"C:\Users\GUTKRISH\chromedriver.exe"
#specify where your chrome driver present in your pc
driver = webdriver.Chrome(PATH)
#provide website url here
driver.get("https://omayo.blogspot.com/")
time.sleep(2)
#finds element by id
alert_button = driver.find_element_by_id("alert1")
#click the button
# alert_button.click()
try:
#switch to alert and click ok button
driver.switch_to.alert.accept()
except NoAlertPresentException:
print("exception hanlded")
print("Rest of the programm")
Attributions:
  1. undefined by undefined