记录使用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. SQL Server database – Error 3743

    Database mirroring must be removed before you drop SQL Server database – Error 3743 If you try to dr ...

  2. MySQL数据库优化(一)

    1.1.1. 慢查询日志当查询超过一定的时间没有返回结果的时候,才会记录到慢查询日志中.默认不开启.采样的时候手工开启.可以帮助我们找出执行慢的 SQL 语句查看慢 SQL 日志是否启用(on 表示启 ...

  3. Hibernate + mysql 配置

    1.生成po的配置 2.连接 MySQL 数据库的配合

  4. 深入浅出Mybatis系列八-mapper映射文件配置之select、resultMap

    注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之inse ...

  5. 48.Python中ORM模型实现mysql数据库基本的增删改查操作

    首先需要配置settings.py文件中的DATABASES与数据库的连接信息, DATABASES = { 'default': { 'ENGINE': 'django.db.backends.my ...

  6. HTML-入门

    HTML——超文本标记语言(Hyper Text Markup Language)html——W3C制定的标准超文本——除了文本,还有图片.视频.音频等丰富的元素标记(标签)——用来展示内容表型:标签 ...

  7. 本机添加多IP绑定网站

    查询IP 显示为1个IP 点击更改适配器 点击高级 进行添加IP 点击添加 ipconfig 查看效果 注~!:在IIS中可以在这里添加多IP然后绑定

  8. Centos7安装gitlab-ce

    1.官方推荐方式安装 参考https://www.gitlab.com.cn/installation/#centos-7?version=ce sudo yum install -y curl po ...

  9. Selenium3+python自动化009-iframe定位

    iframe 一.frame:HTML页面中的一种框架,主要作用是在当前页面中指定区域显示另一页面元素: 二.操作Frame中的页面元素 定位元素: 1.id定位driver.switch_to.fr ...

  10. SpringMVC组件解析

    SpringMVC组件解析 1. 前端控制器:DispatcherServlet 用户请求到达前端控制器,它就相当于 MVC 模式中的 C,DispatcherServlet 是整个流程控制的中心,由 ...