How do I recover the “text” from the page originating the alert, esp **after** the human user has *clicked* [dismiss] on the page’s **alert**?

I have been struggling with this issue off and on for a long time; your comment on your question solved the problem for me:

After both UnexpectedAlertPresentException and NoAlertPresentException are thrown…

browser.execute_script('alert("Clearing out past dialogs.")')
browser.switch_to.alert.accept()

As you said in your answer, webdriver is creating a ‘dialog’ when the alert is present. Closing the alert by hand causes its reference to get lost in limbo, but it’s still blocking access to body.text. Creating a new alert seems to allow webdriver to clear out that old ‘dialog’ and (after accepting) grants access to the page again.

Leave a Comment