使用python析构函数销毁webdriver报错?

郎总 2018-01-22 11:24:49
正在学习使用webdriver,自己做了个实验,代码如下,我的想法就是在A的实例销毁的时候将浏览器关闭,保持二者生命周期一致。但是运行结果却报错了,自己猜想,可能是浏览器打开关闭需要时间,而a销毁的很快,二者无法同步吗?这个问题有好的解决方案吗?

from selenium import webdriver
class A():
def __init__(self):
print("this a A consctructer")
self.a = webdriver.Chrome(executable_path="/Applications/chromedriver")
self.a.get(url='https://www.baidu.com')

def __del__(self):
print("this a A desctructer")
self.a.quit()

a = A()
--------------------------------------------------------------------------
运行结果
this a A consctructer
this a A desctructer
Exception ignored in: <bound method A.__del__ of <__main__.A object at 0x1041024a8>>
Traceback (most recent call last):
File "/Users/~/Desktop/ScrapyPic/test.py", line 11, in __del__
File "/Users/~/Library/Python/3.6/lib/python/site-packages/selenium/webdriver/chrome/webdriver.py", line 123, in quit
File "/Users/~/Library/Python/3.6/lib/python/site-packages/selenium/webdriver/common/service.py", line 149, in stop
File "/Users/~/Library/Python/3.6/lib/python/site-packages/selenium/webdriver/common/service.py", line 120, in send_remote_shutdown_command
ImportError: sys.meta_path is None, Python is likely shutting down

Process finished with exit code 0
...全文
441 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

3,423

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧