2015-02-08 30 views
6

我正在使用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> 
+0

如果警报使用(如JAVA)'d.switchTo()警报()接受();'(请找python中的等效绑定)。如果它是一个模态对话框,那么我想你可以尝试找到好的定位器。 – 2015-02-08 18:07:13

+0

@VivekSingh它只是'driver.switch_to_alert.accept()',甚至'driver.switch_to.alert.accept()'。我发现两种方法都略有差异,所以认为对于那些为此而苦苦挣扎的人的建议很重要。 'driver.switch_to.window()'方法同样适用。 – erm3nda 2016-05-24 01:11:11

回答

10

您需要switch to the alert并接受它:。

world.browser.find_element_by_link_text("Delete User").click() 
alert = world.browser.switch_to.alert 
alert.accept() 
+0

它不让我去world.browser.switch_to_alert()。我试过了,它之前没有成功,也不会超越它。 – andygimma 2015-02-09 01:39:45

+0

@andygimma所以,它看起来像在您点击删除用户按钮之前打开了一个警报。 – alecxe 2015-02-09 02:26:02

+0

我有一个错字,你的回答是正确的。虽然我没有足够的声望点来支持你。谢谢! – andygimma 2015-02-09 07:54:34