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
# 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()
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
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
# 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
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.
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.
Achieve More Than 90% Test Automation | |
Step by Step Walkthroughs and Help | |
14 Day Free Trial, Cancel Anytime |