0

I am running a few thousand lines of Python code that invoke Selenium to conduct web parsing. I just started using driver.refresh(), a method which creates an "alert" message that users can accept or reject. I am accepting the alert with the following lines of code:

driver.refresh()
time.sleep(1)
#refresh loads alert; switch focus to that alert
alert = driver.switch_to_alert()
alert.accept()

But the alert.accept() line (which is line 913 in the lengthy code) yields the following Traceback:

Traceback (most recent call last):
  File "2.7.4__next__page__test.py", line 44, in __call__
    return self._func(*args)
  File "2.7.4__next__page__test.py", line 1202, in startapi
    loggingvariableprovided)
  File "2.7.4__next__page__test.py", line 913, in query_literature_online_master_function
    alert.accept()
  File "c:\python27\lib\site-packages\selenium-2.37.2-py2.7.egg\selenium\webdriver\common\alert.py", line 54, in accept
    self.driver.execute(Command.ACCEPT_ALERT)
  File "c:\python27\lib\site-packages\selenium-2.37.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 164, in execute
    self.error_handler.check_response(response)
  File "c:\python27\lib\site-packages\selenium-2.37.2-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'a is null' ; Stacktrace: 
    at fxdriver.modals.findButton_ (file:///c:/users/douglas/appdata/local/temp/tmpco7fll/extensions/fxdriver@googlecode.com/components/driver_component.js:8443)
    at fxdriver.modals.acceptAlert (file:///c:/users/douglas/appdata/local/temp/tmpco7fll/extensions/fxdriver@googlecode.com/components/driver_component.js:8414)
    at FirefoxDriver.prototype.acceptAlert (file:///c:/users/douglas/appdata/local/temp/tmpco7fll/extensions/fxdriver@googlecode.com/components/driver_component.js:9111)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/douglas/appdata/local/temp/tmpco7fll/extensions/fxdriver@googlecode.com/components/command_processor.js:10851)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///c:/users/douglas/appdata/local/temp/tmpco7fll/extensions/fxdriver@googlecode.com/components/command_processor.js:396) 

I'm running Windows 8.1, Python 2.7, Selenium 2.37.2, and Firefox 23 Portable. Does anyone know what this error message means, and what I can do to resolve the snag? I would be grateful for any suggestions others can offer.

Solution @Jason's response led me to the following code from a previous SO answer, which solves the problem in my case:

try:
    WebDriverWait(driver, 3).until(expected_conditions.alert_is_present(),
                                   'Timed out waiting for PA creation ' +
                                   'confirmation popup to appear.')

    alert = driver.switch_to_alert()
    alert.accept()
    print "alert accepted"
except TimeoutException:
    print "no alert"
Community
  • 1
  • 1
duhaime
  • 25,611
  • 17
  • 169
  • 224

2 Answers2

1

Well, WebDriverException with a javascript traceback at the end means it's coming from the browser side. a is probably the reference to the alert. The selenium Alert wrapper class doesn't do any checking, it just sends commands to the browser. Probably the alert is not coming up soon enough. You could wait longer, or catch the exception and try again, or try to check if the alert is up first.

Jason S
  • 13,538
  • 2
  • 37
  • 42
0

Try switching to Active Element instead and Then send in {ENTER} It would be done in C# like this :

driver.SwitchTo().ActiveElement();
System.Windows.Forms.SendKeys.SendWait(@"{ENTER}");