记录使用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. 2020牛客寒假算法基础集训营4-I 匹配星星【贪心】

    链接:https://ac.nowcoder.com/acm/contest/3005/I来源:牛客网 示例1 输入 复制 2 1 1 0 2 2 1 2 1 1 0 2 2 1 输出 复制 1 1 ...

  2. Python标准库之hashlib模块与hmac模块

    hashlib模块用于加密相关的操作.在Python 3.x里代替了md5模块和sha模块,主要提供 SHA1.SHA224.SHA256.SHA384.SHA512 .MD5 算法.如果包含中文字符 ...

  3. VSCode常用插件之EditorConfig for vs code 使用

    更多VSCode插件使用请访问:VSCode常用插件汇总 当大家在公司工作时,不可能永远是一个人维护一个项目,当多个人参与一个项目,每个人使用的编辑器不一样,代码风格自然也不一样,那么如何让使用不同编 ...

  4. Vuejs+elementUI框架开发的项目结构及文件关系

    项目结构|----- build #webpack编译相关文件目录,一般不用动 |----- config #配置目录|         |------ dev.env.js #开发环境变量| |-- ...

  5. Doing Homework HDU - 1074 状态压缩

    #include<iostream> #include<cstring> #include<cstdio> #include<string> #incl ...

  6. 10.3 c++ STL 初步

    #include<Windows.h>#include<iostream>#include<algorithm>  // sort  swap   min   ma ...

  7. java的开发步骤----

    首先老师给了一个 视频给我:叫我理解java的简单开发步骤: 上两张图: 然后我就理解成了这样::: 简述java开发步骤:第一步: 编写源代码,把源代码写好 :第二部:源代码通过编译器编译成字节码c ...

  8. 改善深层神经网络(三)超参数调试、Batch正则化和程序框架

    1.超参数调试: (1)超参数寻找策略: 对于所有超参数遍历求最优参数不可取,因为超参数的个数可能很多,可选的数据过于庞大. 由于最优参数周围的参数也可能比较好,所以可取的方法是:在一定的尺度范围内随 ...

  9. Copy Paste DWG to older 3ds Max

    Hi, This is quick tutorial: how to install Auto Cad scripts to be able to copy from newer Auto Cad t ...

  10. NG-ALAIN 边学边记2

    1. 下载Git上的源码  : https://github.com/ng-alain/ng-alain.git 2.解压文件: 3.进入到目录下: 4.打开CMD 切换到 E:\NgAlain\ng ...