2014-06-16 38 views
0

我在python中有一個簡單的腳本,用於使用selenium web驅動程序登錄到web站點。問題是當我試圖關閉網絡驅動程序時,FireFox給了我一個意外的警報。這是我的腳本:在python中關閉Firefox web驅動程序時出現意外警報

from selenium import webdriver 
from time import * 

class ClassTest(object): 
    def __init__(self): 
     self.driver = webdriver.Firefox() 
     self.driver.get('url') 


    def login(self, username, password): 
     self.driver.find_element_by_name("username").clear() 
     self.driver.find_element_by_name("username").send_keys(username) 
     self.driver.find_element_by_name("password").clear() 
     self.driver.find_element_by_name("password").send_keys(password) 
     self.driver.find_element_by_name("submit").click() 

    def logout(): 
     self.driver.find_element_by_name('logout').click()   

    def __del__(self): 
     self.logout() 
     self.driver.close() 
     self.driver.quit() 

if __name__ == "__main__": 
    test = ClassTest() 
    test.login('myUsername', 'myPasswd') 

我試過兩種方法關閉網頁驅動程序窗口;首先通過使用self.driver.close(),然後在self.driver.close()之後使用self.driver.quit()。兩者都給了我相同的結果。該堆棧跟蹤是如下:

Message: u'Modal dialog present' ; Stacktrace: 
    at nsCommandProcessor.prototype.execute (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/[email protected]/components/command_processor.js:10949) 
    at Dispatcher.executeAs/< (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/[email protected]/components/driver_component.js:7717) 
    at Resource.prototype.handle (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/[email protected]/components/driver_component.js:7864) 
    at Dispatcher.prototype.dispatch (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/[email protected]/components/driver_component.js:7811) 
    at WebDriverServer/<.handle (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/[email protected]/components/driver_component.js:10740) 
    at <anonymous> (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/[email protected]/components/httpd.js:1935) 
    at ServerHandler.prototype.handleResponse (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/[email protected]/components/httpd.js:2261) 
    at Connection.prototype.process (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/[email protected]/components/httpd.js:1168) 
    at RequestReader.prototype._handleResponse (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/[email protected]/components/httpd.js:1616) 
    at RequestReader.prototype._processBody (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/[email protected]/components/httpd.js:1464) 
    at RequestReader.prototype.onInputStreamReady (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/[email protected]/components/httpd.js:1333) 

這是警告:

enter image description here

這是什麼緣故任何想法???

編輯1:

我已經有一些解決方法,我發現,只要我不註銷從的網站,沒有警報。也許註銷按鈕或後面的進程有問題。它的HTML代碼是這樣的:

<img src="/images/button/exit.png" class="gwt-Image" name="logout" title="Log Off"> 

回答

0

我相信當一個AJAX調用被中斷時會顯示對話框。我通常在Chrome和Firefox中看到它。

一些建議。

註銷後進入睡眠狀態。我相信在註銷期間可能會運行AJAX,並且使用短暫的睡眠可能會讓AJAX運行完成。

import time 

self.logout() 
time.sleep(5) 
self.driver.close() 
self.driver.quit() 

包裝一個alert.accept()try-except

self.logout() 
try: 
    alert = self.driver.switch_to_alert() 
    alert.accept() 
    print "alert accepted" 
except TimeoutException: 
    print "no alert" 
self.driver.close() 
self.driver.quit() 
+0

謝謝,但我已經知道這個概念@Richard,我想知道這種現象的確切原因。 –

相關問題