27

I am running basic CRUD tests with python and lettuce using selenium webdriver.

All of my other tests run fine, but when I click "Delete", there is a modal dialog which asks the user to confirm that they want to delete that user. The moment the popup appears, I get the exception below. I have wasted 2 hours trying to get this to work. I would imagine there is a simple fix.

 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 164, in check_response
    raise exception_class(message, screen, stacktrace)
UnexpectedAlertPresentException: Message: u'Modal dialog present' ; Stacktrace: 
    at nsCommandProcessor.prototype.execute (file:///tmp/tmpeV2K89/extensions/[email protected]/components/command_processor.js:11520:13)

The line in steps.py that is throwing the error is this.

  world.browser.find_element_by_link_text("Delete User").click()

And the html for that element is this.

<a href="/users/5910974510923776/delete" onclick="return confirm('Are you sure you want to delete this user?');">Delete User</a>
2
  • If its an alert use (as in java) d.switchTo().alert().accept(); (please find equivalent binding in python). If its a modal dialog then i guess u can try finding the locator of ok. Commented Feb 8, 2015 at 18:07
  • @VivekSingh it's just driver.switch_to_alert.accept() or even driver.switch_to.alert.accept(). I've found slightly differences on both mehods so thought that's important to advice for anyone struggling around that. The same thing applies to the driver.switch_to.window() method. Commented May 24, 2016 at 1:11

1 Answer 1

37

You need to switch to the alert and accept it:

world.browser.find_element_by_link_text("Delete User").click()
alert = world.browser.switch_to.alert
alert.accept()
Sign up to request clarification or add additional context in comments.

7 Comments

It's not letting me get to world.browser.switch_to_alert(). I tried that, it's failing on the line before and won't go beyond it.
@andygimma so, it looks like there is an alert opened before you click on the Delete User button.
I had a typo, your answer was correct. I don't have enough reputation points to upvote you though. Thanks!
@1nflktd good point, though it should be .switch_to.alert(). (no need to call switch_to ). Thanks.
@alecxe: Should it be .switch_to.alert() or just .switch_to.alert? I'm using Python 2.7 and Selenium 2.46.0, and if I try .switch_to.alert(), I will get a "TypeError: 'Alert' object is not callable" exception. The source code shows that alert is decorated by @property so I think we should call it without the parentheses.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.