gpt4 book ai didi

python - 在 python 中关闭 Firefox Web 驱动程序时出现意外警报

转载 作者:太空宇宙 更新时间:2023-11-03 18:20:27 26 4
gpt4 key购买 nike

我有一个简单的 python 脚本,用于使用 selenium web-driver 登录网站。问题是当我尝试关闭网络驱动程序时,FireFox 给了我一个意外的警报。这是我的脚本:

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/fxdriver@googlecode.com/components/command_processor.js:10949)
at Dispatcher.executeAs/< (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/fxdriver@googlecode.com/components/driver_component.js:7717)
at Resource.prototype.handle (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/fxdriver@googlecode.com/components/driver_component.js:7864)
at Dispatcher.prototype.dispatch (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/fxdriver@googlecode.com/components/driver_component.js:7811)
at WebDriverServer/<.handle (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/fxdriver@googlecode.com/components/driver_component.js:10740)
at <anonymous> (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/fxdriver@googlecode.com/components/httpd.js:1935)
at ServerHandler.prototype.handleResponse (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/fxdriver@googlecode.com/components/httpd.js:2261)
at Connection.prototype.process (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/fxdriver@googlecode.com/components/httpd.js:1168)
at RequestReader.prototype._handleResponse (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/fxdriver@googlecode.com/components/httpd.js:1616)
at RequestReader.prototype._processBody (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/fxdriver@googlecode.com/components/httpd.js:1464)
at RequestReader.prototype.onInputStreamReady (file:///c:/users/user/appdata/local/temp/tmpeqmito/extensions/fxdriver@googlecode.com/components/httpd.js:1333)

这是警报:

enter image description here

知道原因吗???

编辑1:

我有一些解决方法,我发现每当我不从网站注销时,就没有警报。也许注销按钮或其背后的过程有问题。它的HTML代码是这样的:

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

最佳答案

我相信当 AJAX 调用中断时会显示对话框。我通常在 Chrome 和 Firefox 中看到它。

一些建议。

注销后短暂 sleep 。我相信 AJAX 可能会在注销时运行,并且使用短暂的 sleep 可能会让 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()

关于python - 在 python 中关闭 Firefox Web 驱动程序时出现意外警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24236966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com