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')



猜你喜欢

转载自blog.csdn.net/Cand6oy/article/details/80986661