簡體   English   中英

Python / Selenium:WebDriverException:消息:u為空'

[英]Python / Selenium: WebDriverException: Message: u'a is null'

我正在運行數千行調用Selenium進行Web解析的Python代碼。 我剛剛開始使用driver.refresh(),這是一種創建“警報”消息的方法,用戶可以接受或拒絕該消息。 我接受以下代碼行的警報:

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

但是alert.accept()行(在冗長的代碼中是913行)產生以下回溯:

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) 

我正在運行Windows 8.1,Python 2.7,Selenium 2.37.2和Firefox 23 Portable。 有誰知道此錯誤消息的含義,以及如何解決此故障? 對於其他人可以提供的任何建議,我將不勝感激。

解決方案 @Jason的響應使我從先前的SO答案中獲得了以下代碼,從而解決了我的問題:

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"

好吧, WebDriverException的末尾帶有JavaScript追溯意味着它來自瀏覽器端。 a可能是對該警報的引用。 selenium Alert包裝器類不執行任何檢查,它只是將命令發送到瀏覽器。 警報可能還不夠快。 您可以等待更長的時間,或者捕獲異常然后重試,或者嘗試首先檢查警報是否啟動。

嘗試改用Active Element,然后在{ENTER}中發送,將在C#中完成,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM