相关文章推荐
焦虑的双杠  ·  Shader Storage ...·  1 年前    · 
叛逆的花生  ·  Troubleshoot ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Python: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None Message:

Ask Question

So i have been facing this problem in my python projects; whenever a javascript alert appears on my page(while working on the python CLI interpreter for ubuntu linux), every other instruction statement throws me this error

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None Message:

i mean even simple instructions such as browser.current_url as well as complex ones too.

if i try to switch to the alert and accept it using

browser.switch_to.alert.accept()

this error comes up:

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoAlertPresentException: Message: No modal dialog is currently open

i usually end up closing the browser and restarting the test all over again. This is stressful.

i am using ubuntu linux 16.04 LTS + python 2.7 + selenium version = '3.4.3'

Suspect it is because the alert is not immediately displayed and the switch alert is done quicker than the alert being displayed, you will end up having a NoAlertPresentException.

Perhaps you can try this:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
browser.find_element_by_id('show-alert').click()
    WebDriverWait(browser, 5).until(EC.alert_is_present(), 'Timed out waiting for alerts to appear')
    alert = browser.switch_to.alert
    alert.accept()
except TimeoutException:
    print ("Timeout and No Alert Appearing")
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.