记录使用python调用chromedriver时遇到的问题

代码:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# author : fy
# version : V1.0
# file: chrome_test.py
# time:2018/10/30 10:19 from selenium import webdriver
import platform class CT(object):
def __init__(self):
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument("--no-sandbox")
options.add_argument("--lang=" + "zh-CN")
options.add_argument(
'--user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')
pf = platform.system()
if pf == 'Windows':
chromedriver = "E:\\ChromeDownload\\chromedriver_win32\\chromedriver.exe"
else:
chromedriver = "/usr/bin/chromedriver" self.obj = webdriver.Chrome(chrome_options=options, executable_path=chromedriver)
def __del__(self):
self.obj.quit()
pass def openpage(self):
self.obj.get('http://www.baidu.com')
print self.obj.page_source
#self.obj.close()
if __name__=='__main__':
cct = CT()
cct.openpage()
其中:
self.obj.close() ---关闭浏览器当前打开的页面,chromedriver进程不会退出
self.obj.quit()     ---浏览器进程退出,任务管理器中可以看到chromedriver进程被释放

如果直接这样运行会出现报错:

Exception RuntimeError: RuntimeError('sys.meta_path must be a list of import hooks',) in <bound method CT.__del__ of <__main__.CT object at 0x040F61F0>> ignored

解决方法1:
把__del__中的self.obj.quit()放到实现函数里面
    def openpage(self):
self.obj.get('http://www.baidu.com')
print self.obj.page_source
self.obj.quit()

但是如果实现函数中有很多分支,每个分支中如果出现异常,都要调用quit很麻烦

解决方法2:

保留__del__中的self.obj.quit(),在外面手动释放类实例del cct

if __name__=='__main__':
cct = CT()
cct.openpage()
del cct

至于原因,在网上查了半天也没找到合适的解释。。。

selenium chromedriver退出报错的更多相关文章

  1. PyCharm+selenium环境搭建报错:Traceback (most recent call last): TypeError: 'module' object is not callable

    环境搭建好后,代码如下: from selenium import webdriverdriver = webdriver.chrome()driver.get("http://www.ba ...

  2. python脚本中selenium启动浏览器报错os.path.basename(self.path), self.start_error_message) selenium.common.excep

    在python脚本中,使用selenium启动浏览器报错,原因是未安装浏览器驱动,报错内容如下: # -*- coding:utf-8 -*-from selenium import webdrive ...

  3. Selenium Grid 运行报错 Exception thrown in Navigator.Start first time ->Error forwarding the new session Empty pool of VM for setup Capabilities

    Selenium Grid 运行报错 : Exception thrown in Navigator.Start first time ->Error forwarding the new se ...

  4. selenium执行js报错

    selenium执行js报错 Traceback (most recent call last):    dr.execute_script(js)  File "C:\Python27\l ...

  5. python2.7运行selenium webdriver api报错Unable to find a matching set of capabilities

    在火狐浏览器33版本,python2.7运行selenium webdriver api报错:SessionNotCreatedException: Message: Unable to find a ...

  6. python中用selenium调Firefox报错问题

    python在用selenium调Firefox时报错: Traceback (most recent call last):  File "G:\python_work\chapter11 ...

  7. Selenium打开IE报错“Protected Mode settings...”解决方法

    最近在使用Selenium打开IE浏览器碰到以下报错:

  8. selenium IDE 回放报错

    解决:Selenium RC未启动,启动即可. java -jar selenium-server-standalone-2.25.0.jar 启动RC报错,提示找不到firefox的path,于是配 ...

  9. pip安装selenium时,报错“You are using pip version 10.0.1, however version 18.0 is available.”的问题

    pip安装selenium,pip install selenium 类型这样错误 1  原因可能不是以管理员身份运行cmd安装selenium 2  解决方式 也是要管理员身份运行 重点在最后一句 ...

随机推荐

  1. H5-设置全屏背景图片样式

    .bgimg{ width: 100%; height: 95vh; margin: 0; padding: 0 .32rem; background-image: url('../image/ld. ...

  2. [CF1303D] Fill The Bag - 贪心

    Solution 考虑从低位往高位贪心,设当前在处理第 \(i\) 位,更低位剩余的部分一共可以拼出 \(cnt\) 个 \(2^i\) 如果 \(n\) 的这一位是 \(1\) ,那么这一位就需要处 ...

  3. Conference deadlines

    1. NLP/IR/DM/ML Conference Deadlines(Updating) Two Principles of Deadlines:1. All deadlines converge ...

  4. JS添加和删除表格行

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  5. PP: Shape and time distortion loss for training deep time series forecasting models

    Problem: time series forecasting Challenge: forecasting for non-stationary signals and multiple futu ...

  6. PHP对一维数组去重

    一维数组去重 $arr =[1,2,2,3,6]; $arr1 =array_flip($arr); $arr =array_flip($arr1); return $arr; array_flip( ...

  7. [转]memory analyzer 使用方法

    [转]http://wensong.iteye.com/blog/1986449 最近一段时间一直在研究热部署,热部署中涉及到一个比较头痛的问题就是查内存泄露(Memory Leak),于是乎在研究热 ...

  8. AE接口编程

    [转]原文链接:https://malagis.com/arcgis-engine-10-develop-handbook-2-1.html 使用 ArcGIS Engine,也就意味着使用里面的接口 ...

  9. Windows DOS下查看硬盘分区

    运行cmd Diskpart //加载 list Disk //列出硬盘 list Volume //列出分区 Select Disk //选择 编号0的硬盘 list Partition //列出当 ...

  10. BibTex 学习笔记

    BibTex 学习笔记 使用BibTex 来管理参考文献:一次管理,终身使用! 1. 定义 BibTeX 是一个使用数据库的的方式来管理参考文献程序, 用于协调LaTeX的参考文献处理. BibTeX ...