我正在使用selenium webdriver运行基本的CRUD测试,使用python和生菜。Python Selenium UnexpectedAlertPresentException
我所有的其他测试运行良好,但是当我点击“删除”时,会出现一个模式对话框,要求用户确认他们想要删除该用户。弹出窗口出现时,我会看到下面的例外情况。我浪费了2个小时试图让这个工作。我会想象有一个简单的修复。
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)
正在抛出错误的steps.py中的行是这样的。
world.browser.find_element_by_link_text("Delete User").click()
该元素的html是这个。
<a href="https://stackoverflow.com/users/5910974510923776/delete" onclick="return confirm('Are you sure you want to delete this user?');">Delete User</a>
如果警报使用(如JAVA)'d.switchTo()警报()接受();'(请找python中的等效绑定)。如果它是一个模态对话框,那么我想你可以尝试找到好的定位器。 – 2015-02-08 18:07:13
@VivekSingh它只是'driver.switch_to_alert.accept()',甚至'driver.switch_to.alert.accept()'。我发现两种方法都略有差异,所以认为对于那些为此而苦苦挣扎的人的建议很重要。 'driver.switch_to.window()'方法同样适用。 – erm3nda 2016-05-24 01:11:11