Turn your manual testers into automation experts! Request a DemoStart testRigor Free

How to Fix NoAlertPresentException in Selenium?

An Alert is a small box that appears on the UI to provide some information to the user or to seek the user’s response in the form of Yes/No/Cancel/OK/Text, etc. They are also known as ‘Dialog-Box’. They appear as a pop-up on the screen and wait for the user to respond before proceeding with the rest of the operations on the UI. They can appear as:

  • Alert Box: This simple dialog box has only one button, “OK”, and provides some information to the user. The user clicks “OK” as a response.
  • Confirmation Box: This dialog box asks for confirmation or cancellation of an action. It may have two buttons, “OK” and “Cancel”.
  • Prompt Box: Asks for some text input from the user, and then the user clicks some button to confirm or cancel the information.

NoAlertPresentException extends NotFoundException, indicating that a WebDriver has tried to interact with an alert/ warning/ prompt/confirmation box that is not present on the screen. First, the Selenium script needs to switch the focus on the alert box and perform the action, such as clicking “OK”. Switching to the non-existent (may be delayed) alert pop-up will result in NoAlertPresentException. It is a pretty common scenario that can be avoided easily.

The common reasons for this exception are:

  • Timing Issues: The Selenium script is trying to interact with the alert before it appears or after it has already appeared and vanished.
  • Incorrect Alert Handling: The alert is triggered. However, the Selenium script is handling it incorrectly.
  • No Alert is Triggered: The Selenium script expects the alert to appear after a user action. But actually, the user action has not triggered it.

Resolution to NoAlertPresentException in Selenium

Below are a few ways to handle NoAlertPresentException:

Wait for the Alert to be Present Before Interaction

If you try to interact with the alert too quickly before it appears, the exception will be thrown, as shown below:
# Example: Interacting with an alert without waiting for it to appear
driver.find_element_by_id("trigger_alert_button").click()
alert = driver.switch_to.alert
alert.accept()
Use explicit wait for the alert to be present on UI before the Selenium script interacts with it. In the example below, WebDriver will wait 10 seconds for the alert to be present on the screen before it interacts with it.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.find_element_by_id("trigger_alert_button").click()
WebDriverWait(driver, 10).until(EC.alert_is_present())
alert = driver.switch_to.alert
alert.accept()

Use try-except or try-catch Blocks

Use try-except (in Python) or try-catch (in Java) to handle the NoAlertPresentException. Check beforehand that the exception is present before interacting with it:
from selenium.common.exceptions import NoAlertPresentException
try:
  alert = driver.switch_to.alert
  alert.accept() # or other interactions
except NoAlertPresentException:
  print("No alert present at this moment.")

Manage Alert Trigger Event Correctly

You need to make sure that the event (user action) triggering the alert is correctly implemented in the code. For example, a UI prompt alert is present, requiring some text to be sent as a response. Then, the user clicks the “OK” button to accept the alert. If the script directly accepts the alert without providing any text as input, such handling is incorrect and may trigger the exception as shown below:
# Example: Incorrectly handling a prompt
driver.find_element_by_id("trigger_prompt").click()
alert = driver.switch_to.alert
alert.accept() # Prompt requires input before accepting
The fix for this issue is to provide the text input and then accept the alert for a smooth alert event trigger and further processing. The correct code is as follows:
alert = driver.switch_to.alert
alert.send_keys("Some text")
alert.accept()

How AI Handles NoAlertPresentException?

Handling so many exceptions in coding is time-consuming and annoying when writing test scripts using Selenium. That is the main reason why organizations are looking for better alternatives. Here are the 11 reasons why not to use Selenium.

AI-powered intelligent test automation tools such as testRigor have introduced remarkable freedom to the teams. Its no-code test automation allows you to write test cases in plain English. Team members can write and execute test cases through NLP and generative AI.

testRigor’s intelligent capabilities handle the waits and element presence automatically. You need not introduce explicit waits to handle the element visibility. Can it get better than this? Here is more: the self-healing capabilities automatically incorporate the apparent UI and element attribute changes in the test scripts. This minimizes your team’s maintenance effort, and they use their expertise in building more robust tests.

Here is an example of a testRigor test case in plain English. You need not identify UI elements with XPath/CSS paths. Just use the UI element text that you see on the UI. Here is an example:
enter "TV" into "Search"
enter enter
click "TV"
click "Add To Cart"
check that page contains "Item Added To Cart"

You can also use the recorder to record your UI actions, and testRigor generates test cases in plain English.

Easily integrate testRigor into your existing testing workflow, as testRigor supports integrations with all significant test management, test reporting, ERP, CRM, and infrastructure providers.

Conclusion

With the current Agile and DevOps environments, can you afford to lose time managing unexpected exceptions and errors? Do you wish to save time on enormous maintenance effort when the element attribute changes or there are functional changes?

Today, you need modern tools for modern problems. Use AI-powered codeless test automation to solve the majority of your testing problems. testRigor is a great companion that aids your team in having excellent quality, test scalability, and quickly rolling out maximum test coverage within budget.

You're 15 Minutes Away From Automated Test Maintenance and Fewer Bugs in Production
Simply fill out your information and create your first test suite in seconds, with AI to help you do it easily and quickly.
Achieve More Than 90% Test Automation
Step by Step Walkthroughs and Help
14 Day Free Trial, Cancel Anytime
“We spent so much time on maintenance when using Selenium, and we spend nearly zero time with maintenance using testRigor.”
Keith Powe VP Of Engineering - IDT
Related Articles

SAP Concur Testing

Concur Technologies, now SAP Concur, is an American software-as-a-service (SaaS) company providing travel and expense management ...

Authorize.net Testing

You need a payment gateway in several scenarios, especially when you are running a business that requires secure, efficient, and ...

KYC Portal CLM Testing

KYC Portal CLM is an award-winning Client Lifecycle Management (CLM) platform designed to streamline and automate the Know Your ...
On our website, we utilize cookies to ensure that your browsing experience is tailored to your preferences and needs. By clicking "Accept," you agree to the use of all cookies. Learn more.
Cookie settings
Privacy Overview
This site utilizes cookies to enhance your browsing experience. Among these, essential cookies are stored on your browser as they are necessary for ...
Read more
Strictly Necessary CookiesAlways Enabled
Essential cookies are crucial for the proper functioning and security of the website.
Non-NecessaryEnabled
Cookies that are not essential for the website's functionality but are employed to gather additional data. You can choose to opt out by using this toggle switch. These cookies gather data for analytics and performance tracking purposes.