记录使用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. 选课系统项目_python

    一.功能简要 基本实现以下功能,但是有部分地方由于时间关系并未写,而且并未做细微的完善,大致功能完成.角色:学校.学员.课程.讲师要求:1. 创建北京.上海 2 所学校2. 创建linux , pyt ...

  2. GNU Radio无线通信嗅探基础

    文章内容简介 1.使用哪些grc模块完成我们的嗅探工作 2.如何选择参数以获取最完美的波形 3.如何从波形还原回数据 我接下来会使用电视棒(RTL-SDR)嗅探一个固定码遥控锁开发组件. 我使用如下的 ...

  3. jquery赋值

    $("#test1").text("Hello world!"); $("#test2").html("<b>Hell ...

  4. IDAE打包WEB项目 WAR Eclipse转IDEA项目

    接下来这里只说WEB项目打包,相信大多数都是用的WEB项目吧,关于打包WAR,真的很头痛,网上说的试了好好次都不行. 后来懂了之后,真的很简单好么,分享给大家   不要多走弯路了. 注意:   如果你 ...

  5. deepin开机自动启动服务备忘

    systemctl enable mysql.service(设置开机自启) sudo systemctl start nginx.service sudo systemctl restart ngi ...

  6. host文件无写权限时,怎么设置

    点击文件属性---安全---选择对应的用户---编辑属性---勾选需要的属性---应用---确定

  7. cursor 把鼠标指针的形状弄成一只伸出食指的手

    <span style="cursor:auto">auto</span><br> <span style="cursor:cr ...

  8. Three.js的开始(附代码)_2

    1 下载Three.js代码 https://github.com/mrdoob/three.js/tree/master/build 2 引用方法 在HTML中添加以下代码: <script ...

  9. 关于print()、sys.stdout、sys.stderr的一些理解

    print() 方法的语法: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) 其中file = sys.stdout的 ...

  10. PLSQL无法连接(不存在或找不到oci.dll)

    问题说明:新系统安装plsql后,连接不上Oracle,连接时出现过两种报错 1.找不到OCI.dll文件 2.不能初始化OCI.dll文件,即OCI.dll文件错误 解决方案 plsql连接Orac ...