Selenium不同版本配置自动下载驱动及打包细节

Selenium配置浏览器驱动

笔者从接触Python自动化开始就经常使用Selenium进行一些浏览器端界面自动化操作,随后发现不同Selenium版本的迭代更新后相同代码在不同版本无法实现自动化下载浏览器驱动(经查Selenium新版自动下载的浏览器驱动访问地址为谷歌地址,而国内网站不支持访问谷歌地址),故有此篇记录自动下载驱动的代码及selenium版本

自动下载浏览器驱动的方法 selenium4.7.0

python 复制代码
# selenium版本为4.7.0
# 下载位置:C:\Users\Administrator\.wdm\drivers\chromedriver\

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get("https://www.baidu.com")
driver.find_element(By.ID, "kw").send_keys("Selenium"+Keys.RETURN)
input("按回车键退出~")

自动下载浏览器驱动的方法 selenium4.11.0 或4.11.1

python 复制代码
# selenium版本为4.11.0 或4.11.1
# 默认下载位置:C:\Users\Administrator\.cache\selenium\chromedriver

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element(By.ID, "kw").send_keys("Selenium"+Keys.RETURN)
input("按回车键退出~")

手动设置浏览器驱动路径的方法

浏览器驱动下载地址:[浏览器驱动下载地址]:

python 复制代码
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
service = webdriver.ChromeService(executable_path="你的浏览器驱动的路径")
driver = webdriver.Chrome(service=service)
driver.get("https://www.baidu.com")
driver.find_element(By.ID, "kw").send_keys("Selenium"+Keys.RETURN)
input("按回车键退出~")

pyinstaller打包程序时同时打包ChromeDriver

在使用pyinstaller打包程序时,由于手动下载驱动的情况下并不会打包浏览器驱动,所以在打包后的程序复制到别处运行时还需要加上浏览器驱动一起拷贝,所以更建议打包时将手动下载的浏览器驱动一起打包至程序中,使用方法如下:

chromedriver路径需要sys._MEIPASS的路径进行引用

sys._MEIPASS为exe文件运行时生成的临时文件,程序运行结束后即可自动回收清理,不会占用存储

python 复制代码
import os
from selenium import webdriver
import sys

if getattr(sys, 'frozen', False):
    print(sys._MEIPASS+ "./test/chromedriver.exe")
    application_path = sys._MEIPASS+ "你的chromedriver相对路径"
else:
    application_path = os.path.dirname(os.path.abspath(__file__)) + "你的chromedriver相对路径"
service = webdriver.ChromeService(executable_path=application_path)
driver = webdriver.Chrome(service=service)
driver.get("https://www.baidu.com")
print(driver.page_source)
input("输入。。。")

方法一:通过--add-data命令

注意:这里chromedriver.exe 末尾添加的.为当前目录,则该exe要放到test.py同一目录下

pyinstaller -F --add-data "./test/chromedriver.exe;./test" .\test.py

方法二:通过修改 .spec

spec文件需要在datas里设置chromedriver.exe的路径

注意datas是一个元组的列表,格式为 [("源地址", "目的地址"),("源地址", "目的地址")...]

spec 复制代码
# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['test.py'],
    pathex=[],
    binaries=[],
    datas=[('./test/chromedriver.exe','./test')],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='test',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

随后在终端运行pyinstaller.exe ./test.spec即可自动编译(需注意: .spec是pyinstaller第一次编译后产生的文件,上述操作是对该文件修改后进行的再次编译,如果还没有.spec,可以先执行pyinstaller -F xxx.py 生成)

相关推荐
A 计算机毕业设计-小途10 分钟前
大四零基础用Vue+ElementUI一周做完化妆品推荐系统?
java·大数据·hadoop·python·spark·毕业设计·毕设
敬业小码哥3 小时前
记一次:postman请求下载文件的使用方法
测试工具·postman
念念01074 小时前
数学建模竞赛中评价类相关模型
python·数学建模·因子分析·topsis
云天徽上4 小时前
【数据可视化-94】2025 亚洲杯总决赛数据可视化分析:澳大利亚队 vs 中国队
python·信息可视化·数据挖掘·数据分析·数据可视化·pyecharts
☺����5 小时前
实现自己的AI视频监控系统-第一章-视频拉流与解码2
开发语言·人工智能·python·音视频
王者鳜錸5 小时前
PYTHON让繁琐的工作自动化-函数
开发语言·python·自动化
xiao助阵6 小时前
python实现梅尔频率倒谱系数(MFCC) 除了傅里叶变换和离散余弦变换
开发语言·python
麻辣清汤7 小时前
结合BI多维度异常分析(日期-> 商家/渠道->日期(商家/渠道))
数据库·python·sql·finebi
钢铁男儿8 小时前
Python 正则表达式(正则表达式和Python 语言)
python·mysql·正则表达式
钢铁男儿8 小时前
Python 正则表达式实战:解析系统登录与进程信息
开发语言·python·正则表达式