selenium chromedriver退出报错
记录使用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退出报错的更多相关文章
- PyCharm+selenium环境搭建报错:Traceback (most recent call last): TypeError: 'module' object is not callable
环境搭建好后,代码如下: from selenium import webdriverdriver = webdriver.chrome()driver.get("http://www.ba ...
- python脚本中selenium启动浏览器报错os.path.basename(self.path), self.start_error_message) selenium.common.excep
在python脚本中,使用selenium启动浏览器报错,原因是未安装浏览器驱动,报错内容如下: # -*- coding:utf-8 -*-from selenium import webdrive ...
- 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 ...
- selenium执行js报错
selenium执行js报错 Traceback (most recent call last): dr.execute_script(js) File "C:\Python27\l ...
- 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 ...
- python中用selenium调Firefox报错问题
python在用selenium调Firefox时报错: Traceback (most recent call last): File "G:\python_work\chapter11 ...
- Selenium打开IE报错“Protected Mode settings...”解决方法
最近在使用Selenium打开IE浏览器碰到以下报错:
- selenium IDE 回放报错
解决:Selenium RC未启动,启动即可. java -jar selenium-server-standalone-2.25.0.jar 启动RC报错,提示找不到firefox的path,于是配 ...
- 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 解决方式 也是要管理员身份运行 重点在最后一句 ...
随机推荐
- PHPMailer发送邮件遇坑小记
一:phpmailer发送邮件成功了 数据库发送状态也更改 但是用户就是没收到邮件. 出现原因:发送邮件太多 导致邮箱服务器被腾讯封了 发送的邮件统统进入了邮件服务器的草稿箱里. 解决方案: 重新修改 ...
- C语言实现 循环队列
#include <stdio.h>#include <stdlib.h>#include <stdbool.h> typedef struct queue{ in ...
- H5_0019:JS中定义json结构
"7HKm": function(e, a, d) { "use strict"; Object ...
- Python论做游戏外挂,Python输过谁?
玩过电脑游戏的同学对于外挂肯定不陌生,但是你在用外挂的时候有没有想过如何做一个外挂呢? 我打开了4399小游戏网,点开了一个不知名的游戏,唔,做寿司的,有材料在一边,客人过来后说出他们的要求,你按照菜 ...
- 为什么 MySQL 使用 B+ 树
为什么 MySQL 使用 B+ 树是面试中经常会出现的问题,很多人对于这个问题可能都有一些自己的理解,但是多数的回答都不够完整和准确,大多数人都只会简单说一下 B+ 树和 B 树的区别,但是都没有真正 ...
- 仿ios按钮切换
<div> <label><input class="btn-switch" type="checkbox"> 默认未选中& ...
- 如何与GitHub同步,将本地文件push到到远程仓库
Run git config --global user.email "you@example.com" git config --global user.name "Y ...
- c++ stl在竞赛里的使用总结
SET bzoj2761: [JLOI2011]不重复数字 这题... count() 的用法,返回这个值出现的次数,但是在set里只会出现0次和1次,这个可以判断某个值是否在set里出现过 还有si ...
- vue.js中使用离线检测
Html5在window.navigator对象上添加了一个属性onLine 返回布尔值 true表示在线.同时新增了两个事件: window.addEventListener('online', f ...
- Qt Gui 第三章~第四章
一.图片资源的调用 如上图是我图片存放的位置跟qrc的命名: 如下则是qrc对应的编写格式: <RCC> <qresource> <file>images/icon ...