python selenium chrome114版本之后环境配置和携带缓存打开chrome

尽力局

chrome驱动环境配置

网上找到的资料,我现在安装的是120版本的,这个资料是可行的。比较简单
Selenium安装WebDriver最新Chrome驱动(含116/117/118/119)

114之前的版本驱动地址

chrome打开带缓存

说明:这种启动浏览器的方式是相当于手动打开谷歌,带缓存信息

设置待缓存打开

首先查看谷歌信息

chrome://version/

注意这里目录到User Data这就可以了。后面那个不要写进来,空格也不要去掉

executable_path=chromedriver_path这个是驱动地址,环境配置里面会说这是干什么的

python 复制代码
chrome_data_path = r'C:\Users\Cassie\AppData\Local\Google\Chrome\User Data'
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=' + chrome_data_path)
driver = webdriver.Chrome(executable_path=chromedriver_path, options=options)

自动关闭浏览器

说明:这种打开方式需要把现有的谷歌关闭才可以不然会报错

python 复制代码
   ## 杀掉谷歌进程,防止报错
    os.system('taskkill /im chromedriver.exe /F')
    os.system('taskkill /im chrome.exe /F')

出现这些提示不用管

自动关闭浏览器弹窗

这种关闭谷歌之后再次打开会出现这个弹窗,这和网上的那些关弹窗的方法不是一个东西,需要修改配置文件才可以

这里有个大佬的博客解决了这个问题
Python阻止要恢复页面吗/Chrome未正确关闭弹窗

最终代码

找资料难啊

在网上各种查资料很费劲,找了很久才找到有用的信息,其中在带缓存打开的时候一直失败,过了一段时间发现突然好了没改代码。这是pycharm的bug,清除下它的缓存(我这个是2019版本的,新版本估计不一样,但是也是清除缓存)

最终效果代码

我这边实现了2种启动浏览器的方式,一种是普通的不带缓存啥也没有,一种是带缓存的

driver_.py

python 复制代码
import json
import os

from selenium import webdriver

chromedriver_path = r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"
chrome_data_path = r'C:\Users\Cassie\AppData\Local\Google\Chrome\User Data'

def set_exit_normal():
    # 获取配置文件路径
    preferences_path = os.getenv("LOCALAPPDATA") + r'\Google\Chrome\User Data\Default\Preferences'
    # 读取配置文件
    with open(preferences_path, mode='r', encoding='utf8') as pf:
        preferences_file_text = pf.read()
    # 配置文件文本转json
    preferences_file_json = json.loads(preferences_file_text)
    # 修改退出状态为正常
    preferences_file_json['profile']['exit_type'] = 'Normal'
    # 配置好的json转回文本
    preferences_file_text = json.dumps(preferences_file_json, ensure_ascii=False, separators=(',', ':'))
    # 写入配置文件
    with open(preferences_path, mode='w', encoding='utf8') as pf:
        pf.write(preferences_file_text)

def kill_chrome():
    #杀掉前修改下配置,避免弹出chrome
    set_exit_normal()
    ## 杀掉谷歌进程,防止报错
    os.system('taskkill /im chromedriver.exe /F')
    os.system('taskkill /im chrome.exe /F')

def drivers(cache=''):
    if cache == '':
        driver = webdriver.Chrome(chromedriver_path)
        return driver
    else:
        options = webdriver.ChromeOptions()
        options.add_argument('--user-data-dir=' + chrome_data_path)
        driver = webdriver.Chrome(executable_path=chromedriver_path, options=options)
        return driver

# standard_driver=drivers()

if __name__ == '__main__':
    pass
python 复制代码
from time import sleep
from driver_ import drivers, kill_chrome

try:
    driver = drivers('11')
except:
    kill_chrome()
    driver = drivers('11')
    sleep(2)
driver.get('https://blog.csdn.net/aaaaaaaaanjjj')

依赖包和生成依赖包方法

python是3.7,浏览器版本120

关闭谷歌升级

每次升级还是挺费劲的,先关掉升级吧
关闭谷歌自动升级

相关推荐
jaray2 小时前
PyCharm 2024.3.2 Professional 如何更换 PyPI 镜像源
ide·python·pycharm·pypi 镜像源
Psycho_MrZhang2 小时前
Neo4j Python SDK手册
开发语言·python·neo4j
web3.08889992 小时前
1688图片搜索API,相似商品精准推荐
开发语言·python
少云清2 小时前
【性能测试】15_JMeter _JMeter插件安装使用
开发语言·python·jmeter
光羽隹衡3 小时前
机器学习——TF-IDF实战(红楼梦数据处理)
python·tf-idf
2401_894828124 小时前
从原理到实战:随机森林算法全解析(附 Python 完整代码)
开发语言·python·算法·随机森林
B站计算机毕业设计超人4 小时前
计算机毕业设计Python知识图谱中华古诗词可视化 古诗词情感分析 古诗词智能问答系统 AI大模型自动写诗 大数据毕业设计(源码+LW文档+PPT+讲解)
大数据·人工智能·hadoop·python·机器学习·知识图谱·课程设计
玄同7654 小时前
Python「焚诀」:吞噬所有语法糖的终极修炼手册
开发语言·数据库·人工智能·python·postgresql·自然语言处理·nlp
johnny2334 小时前
Python管理工具:包、版本、环境
python
羽翼.玫瑰4 小时前
关于重装Python失败(本质是未彻底卸载Python)的问题解决方案综述
开发语言·python