添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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:

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:

if button.text == "Run": button.click() alert = self.driver.switch_to_alert() alert.send_keys('1111') alert.dismiss() except NoAlertPresentException:

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.

What i can say to you, is that you will get MORE than just only 1 type of error. So, to make more road-warrior you must try to handle the 2 or 3 most common problems, like timeout, not-present, or unexpected. You must import each of them from selenium.common.exceptions and also add a Except NameOfError block then process it. I see today that i can put more than just one Except , and also learned to pass from your code sample :) Loving python and learning it every day. – m3nda Jun 9, 2015 at 12:35 You have to surround with Try block that ` alert = self.driver.switch_to_alert()`. You're forcing the script to change to an alert that maybe is NOT here. Try this: pastebin.com/02RALuGp – m3nda Jun 26, 2015 at 23:19 I think the problem with this is that the exception handler removes the alert from the browser. It's a bit counterintuitive. – Charlie Clark May 10, 2016 at 17:39 Could this be the root cause? A bypassed call to fxdriver.modals.clearFlag_ ... cf. stackoverflow.com/questions/44568402/… – NevilleDNZ Jun 16, 2017 at 23:57

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

I may not be the best python programmer, because i started with it 1 week ago. I've manage myself to create a small function that will accept ANY alert, and also will do it more alerts.

Chaging the IF to WHILE at line -2 (second from tail) we can handle also consecutive confirmations/alerts. Using IF you can handle replies of confirm() Using WHILE you can handle all the alerts(). If the alert is under a timeout, you have to try "bypass" the alert() on the correct moment.

I've manage to add 2 exception handlers, to bypass uknown alerts() and also to stop when no alert() is present.

import selenium
from selenium import webdriver
import os
import time
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.common.exceptions import NoAlertPresentException
os.system("pkill php")
os.system("php -S localhost:2222 alert.html &")
fire = webdriver.Firefox()
fire.get("http://localhost:2222")
global alert
def alert_accept():
    alert = fire.switch_to_alert()
    print "Aler text:" + alert.text
    alert.accept()
    print "Alert detected, accept it"
    return True
  except UnexpectedAlertPresentException:
    print "Hum..., continue?"
    return False
  except NoAlertPresentException:
    print "No alert here"
    return False
while alert_accept() == True:
  alert_accept()

You cant test it with any website. I've make a local html with some different alerts to digg a little on that.

HTML code:

<script type="text/javascript">
var c = confirm("Do you like kidding yourself?")
if (c == true) {
  alert("true")
} else {
  alert("You're the kidding master, another alert!")
</script>
<script type="text/javascript">
var c = confirm("Do you like kidding yourself?")
if (c == true) {
  alert("true")
} else {
  alert("You're the kidding master, another alert!")
</script>
<script type="text/javascript">
console.log("Running test with timeout")
</script>
<script type="text/javascript">
setTimeout(function(){ 
  var c = confirm("Do you like kidding yourself?")
if (c == true) {
  alert("true")
} else {
  alert("You're the kidding master, another alert!")
 }, 5000)
</script>

Actually, the WHILE nor the IF handles the full page, i guess because the timeout. If you put all at onces it does.

Im pretty sure that this can be done using implicit waits and the expected conditions with a shortest code. If you take a look of the source of alert_is_present you will se nothing than a try:block with return true/false.

For my case I need to click on Upload button without selecting File and check that an alert message is coming or not. But in my case while clicking on Upload button the below exception is coming selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: Message: Modal dialog present In my case alert.accept or alert.dismiss is not working So, I did to press Tab and select the Upload button and Press Enter Key through Python Code. Its working perfect.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.