python selenium 执行完毕关闭chromedriver进程示例

因为使用多次以后发现进程中出现了很多chromedriver的残留,造成卡顿,所以决定优化一下。

这个问题困扰了楼主很久,百度谷歌查来查去都只有java,后面根据java和selenium结合看找出了python如何执行完把chromedriver进程关闭

Python的话控制chromedriver的开启和关闭的包是Service

from selenium.webdriver.chrome.service import Service

创建的时候需要把chromedriver.exe的位置写在Service的XXX部分,需要调用他的命令行方法,不然报错然后启动就可以了

c_service = Service('xxx')
c_service.command_line_args()
c_service.start()
driver = webdriver.Chrome()
driver.get(http://www.baidu.com)

关闭的时候用quit而不是采用close

close只会关闭当前页面,quit会推出驱动别切关闭所关联的所有窗口

最后执行完以后就关闭

driver.quit();c_service.stop()

嫌麻烦也可以直接使用python的os模块执行下面两句话结束进程

os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')

以上这篇python selenium 执行完毕关闭chromedriver进程示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • python selenium 对浏览器标签页进行关闭和切换的方法

    1.关闭浏览器全部标签页 driver.quit() 2.关闭当前标签页(从标签页A打开新的标签页B,关闭标签页A) driver.close() 3.关闭当前标签页(从标签页A打开新的标签页B,关闭标签页B) 可利用浏览器自带的快捷方式对打开的标签进行关闭 Firefox自身的快捷键分别为: Ctrl+t 新建tab Ctrl+w 关闭tab Ctrl+Tab /Ctrl+Page_Up 定位当前标签页的下一个标签页 Ctrl+Shift+Tab/Ctrl+Page_Down 定位当前标签页的

  • Python Selenium 之关闭窗口close与quit的方法

    selenium关闭窗口有两个方法,close与quit,我们稍作研究便知道这两个方法的区别. 1.看源码或API 这是close()的说明: Closes the current window. 关闭当前窗口. 这是quit()的说明: Quits the driver and closes every associated window. 退出驱动并关闭所有关联的窗口. 从这里就很明显的看出来这两个方法的区别,一个关闭当前窗口,一个关闭所有窗口,下面写一小段代码测试一下. 2.代码试验 代码

  • selenium 安装与chromedriver安装的方法步骤

    安装 selenium可以直接可以用pip安装. pip install selenium chromedriver的安装一定要与Chrome的版本一致,不然就不起作用(不要问我是怎么知道的). 有两个下载地址: 1.http://chromedriver.storage.googleapis.com/index.html 2.https://npm.taobao.org/mirrors/chromedriver/ 或者本地下载https://www.jb51.net/softs/538241.

  • python selenium 执行完毕关闭chromedriver进程示例

    因为使用多次以后发现进程中出现了很多chromedriver的残留,造成卡顿,所以决定优化一下. 这个问题困扰了楼主很久,百度谷歌查来查去都只有java,后面根据java和selenium结合看找出了python如何执行完把chromedriver进程关闭 Python的话控制chromedriver的开启和关闭的包是Service from selenium.webdriver.chrome.service import Service 创建的时候需要把chromedriver.exe的位置写

  • Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版)

    selenium操作chrome浏览器需要有ChromeDriver驱动来协助.webdriver中关浏览器关闭有两个方法,一个叫quit,一个叫close. /** * Close the current window, quitting the browser if it's the last window currently open. */ void close(); /** * Quits this driver, closing every associated window. */

  • Selenium执行JavaScript脚本的方法示例

    JavaScript是运行在客户端(浏览器)和服务器端的脚本语言,允许将静态网页转换为交互式网页.可以通过 Python Selenium WebDriver 执行 JavaScript 语句,在Web页面中进行js交互.那么js能做的事,Selenium应该大部分也能做.WebDriver是模拟终端用户的交互,所以就不能点击不可见的元素,有时可见元素也不能点击.在这些情况下,我们就可以通过WebDriver 执行JavaScript来点击或者执行页面元素.本文将介绍如何使用 WebDriver

  • 在python中实现强制关闭线程的示例

    如下所示: import threading import time import inspect import ctypes def _async_raise(tid, exctype): """raises the exception, performs cleanup if needed""" tid = ctypes.c_long(tid) if not inspect.isclass(exctype): exctype = type(e

  • python selenium执行所有测试用例并生成报告的方法

    直接上代码. # -*- coding: utf-8 -*- import time import os import os.path import re import unittest import HTMLTestRunner import shutil shutil.copyfile("setting.ini","../setting.ini") casepaths = [] def createsuite(casepath): testunit = unit

  • python+selenium实现简历自动刷新的示例代码

    本文用到的文件的下载地址 百度网盘链接: https://pan.baidu.com/s/1tmpdEfAZKff5TOMAitUXqQ 提取码: e6at 1 安装Python 和 selenium (1)下载Python 官网地址: https://www.python.org/ftp/python/3.6.8/python-3.6.8-amd64.exe 百度网盘链接如上所示 (2)安装Python 等待安装完毕 (3) 安装 selenium pip install selenium –

  • Python selenium实现微博自动登录的示例代码

    (一)编程环境 操作系统:Win 10 编程语言:Python 3.6 (二)安装selenium 这里使用selenium实现. 如果没有安装过python的selenium库,则安装命令如下 pip install selenium (三)下载ChromeDriver 因为selenium要用到浏览器的驱动,这里我用的是Google Chrome浏览器,所以要先下载ChromeDriver.exe并放到C:\Program Files (x86)\Google\Chrome\Applicat

  • Python selenium抓取微博内容的示例代码

    Selenium简介与安装 Selenium是什么? Selenium也是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Mozilla Suite等. 安装 直接使用pip命令安装即可! pip install selenium Python抓取微博有两种方式,一是通过selenium自动登录后从页面直接爬取,二是通过api. 这里采用selenium的方式. 程序: from selen

  • python+selenium识别验证码并登录的示例代码

    由于工作需要,登录网站需要用到验证码.最初是研究过验证码识别的,但是总是不能获取到我需要的那个验证码.直到这周五,才想起这事来,昨天顺利的解决了. 下面正题: python版本:3.4.3 所需要的代码库:PIL,selenium,tesseract 先上代码: #coding:utf-8 import subprocess from PIL import Image from PIL import ImageOps from selenium import webdriver import t

  • Python Selenium Cookie 绕过验证码实现登录示例代码

    之前介绍过通过cookie 绕过验证码实现登录的方法.这里并不多余,会增加分析和另外一种方法实现登录. 1.思路介绍  1.1.直接看代码,内有详细注释说明 # FileName : Wm_Cookie_Login.py # Author : Adil # DateTime : 2018/3/20 19:47 # SoftWare : PyCharm from selenium import webdriver import time url = 'https://system.address'

随机推荐