【selenium】driver.get打包成exe后上报chrome.exe-损坏的映像

现象

在PyCharm里面selenium代码正常运行,用pyinstaller打包后上报chrome.exe-损坏的映像。C:\windows\SYSTEM32\oldshell1048.dll没有被指定在Windows上运行,或者它包含错误,请尝试使用原始安装介质重新安装程序,或联系你的系统管理员或软件供应商以获取支持。错误状态0x0000428。

原先代码

python 复制代码
options = Options()

options.add_argument('headless')  # 无界面模式

# 反屏蔽
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)

driver = webdriver.Chrome(options=options)
# 禁用浏览器自动化测试工具检测
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
    'source': 'Object.defineProperty(navigator, "webdriver", {get:()=>undefined})'
})
# 设定页面加载限制时间
driver.get(URL)

原因

通过定位发现在driver.get处会报该错误,且使用的是selenium库进行开发的,且在部分电脑环境中未上报该错误。猜测是chromedriver和chrome之间的版本关系,毕竟selenium4不需要默认指定chromedriver路径。

经过查询资料发现了selenium4使用了webdriver-manager,自动配置chrome对应的chromedriver,为此使用webdriver-manager进行适配。

解决办法

python 复制代码
os.environ["WDM_SSL_VERIFY"] = "0"  # 禁用 SSL 证书验证
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

options = Options()

options.add_argument('headless')  # 无界面模式

# 反屏蔽
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)
# 使用 ChromeDriverManager 安装 ChromeDriver,并返回驱动程序的路径
driver_path = ChromeDriverManager().install()
# 创建 ChromeDriver 服务,并指定驱动程序的路径
service = Service(driver_path)
driver = webdriver.Chrome(options=options, service=service)
# 禁用浏览器自动化测试工具检测
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
    'source': 'Object.defineProperty(navigator, "webdriver", {get:()=>undefined})'
})
# 设定页面加载限制时间
driver.get(URL)

后记

超级好用,未详细记录细节,有相同问题可以一起探讨!

相关推荐
泽众云测试6 小时前
2026年自动化测试平台对比:以TestOne为主与国外主流商用平台分析
功能测试·测试工具·自动化测试平台
Luminbox紫创测控7 小时前
车载HUD阳光倒灌测试:GB/T46926标准的太阳光模拟器方案
测试工具·汽车·安全性测试·测试标准
随风而飘1867 小时前
罗德与施瓦茨(R&S)SMB100B中端高性能模拟射频/微波信号发生器
网络·功能测试·科技·测试工具
云空8 小时前
《XUbuntu 终端完整命令大全》
linux·前端·chrome·ubuntu·xubuntu
我的xiaodoujiao10 小时前
使用PyMySQL模块技术操作MySQL数据库
数据库·python·测试工具·mysql
陆枫Larry1 天前
聊聊浏览器指纹,以及和Cookie 的区别
chrome
摸摸芋1 天前
postman(ai篇)
测试工具·postman
St_rive1 天前
selenium元素定位方法
selenium·测试工具
St_rive1 天前
selenium常用等待方式
selenium·测试工具
随风而飘1861 天前
罗德与施瓦茨 FSP13 中高端频谱分析仪
网络·功能测试·测试工具