Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

python - Webdriver error: "No alert is present" after UnexpectedAlertPresentException is thrown

I'm trying to test a webapp I'm developing. I'm using the Firefox driver against Firefox 22.0.

At one point, a modal dialog may pop up (a Javascript prompt()). If it does, I want to enter some text and then dismiss it (click OK).

Here's the relevant code:

try:
    if button.text == "Run":
        button.click()
except UnexpectedAlertPresentException:
    alert = self.driver.switch_to_alert()
    print alert.text
    alert.send_keys('8080')
    alert.dismiss()

The UnexpectedAlertPresentException is being thrown. However, as soon as it tries to execute print alert.text, I get:

`NoAlertPresentException: Message: u'No alert is present'`.

If I remove the print statement, it blows up at alert.send_keys with:

`WebDriverException: Message: u'fxdriver.modals.find_(...) is null'`

I don't get it. Isn't the NoAlertPresentException by definition contradicting the UnexpectedAlertPresentException that was thrown and caused the except block to be executed in the first place?

Edit: Also, I can't for the life of me find any documentation on the UnexpectedAlertPresentException in http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html#documentation

Edit 2: This is what I have now:

try:
    if button.text == "Run":
        button.click()

        alert = self.driver.switch_to_alert()

        alert.send_keys('1111')
        alert.dismiss()

 except NoAlertPresentException:
     pass

However, I'm still seeing this:

WebDriverException: Message: u'fxdriver.modals.find_(...) is null' 

on the line alert.send_keys('8080'). I guess I don't understand why switch_to_alert() doesn't throw NoAlertPresent if there isn't an alert...which is what I'm assuming the WebDriverException is indicating.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think Selenium closes unexpected alerts. Apparently you can change how the firefox driver treats unexpected alerts: How to handle an Alert with "UnexpectedAlertBehaviour" capability in Selenium?

As an alternative, you could check if there is an alert before acting (after all, if you want handle the alert it's not unexpected) like so (Java):

try {
  Alert alert = _driver.switchTo().alert();
  //do stuff with alert
} catch (final NoAlertPresentException e) {
  //do non-alert stuff
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...