0

ANOTHER QUESTION ABOUT SAME TOPIC!!

Previous question about closing the alert: Selenium Webdriver: UnexpectedAlertPresentException

I tried closing the alert but now it gives an error saying no such alert.

Error:

Traceback (most recent call last):
  File "main.py", line 19, in <module>
    alert = driver.switch_to.alert.dismiss() 
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/switch_to.py", line 56, in alert
    alert.text
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/common/alert.py", line 66, in text
    return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoAlertPresentException: Message: no such alert
  (Session info: chrome=91.0.4472.101)
Stacktrace:
#0 0x55ec4c205919 <unknown>

Script:

 driver.find_element(By.NAME,"nickname").send_keys(username+str(x)+Keys.ENTER)
 alert = driver.switch_to.alert.dismiss() 
 webdriver.switch_to_alert().dismiss() 
walker38552
  • 35
  • 2
  • 8

1 Answers1

1

NoAlertPresentException exception is thrown when you are trying to close alert when it is not displayed currently, so check if alert is present in place where you are trying to dismiss it.

If you are not sure if alert will occur or not you can catch exception and ignore it:

        try:
            driver.switch_to.alert.dismiss()
        except NoAlertPresentException:
            print('No alert present')
baton153
  • 11
  • 2
  • It doesn't mark 'NoAlertPresentException' as an error, I already tried using try except to clear it. I think it's because the alert is coming from selenium instead of python? – walker38552 Dec 15 '21 at 19:58