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

后记

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

相关推荐
子夜江寒8 小时前
基于Selenium的自动化Web数据采集实践
selenium·自动化
卓码软件测评8 小时前
第三方CMA/CNAS软件测评机构:【Apifox在Dubbo接口调试和RPC服务测试中的测试应用】
网络·测试工具·性能优化·测试用例
测试老哥8 小时前
2026软件测试面试大全(含答案+文档)
自动化测试·软件测试·python·测试工具·面试·职场和发展·测试用例
私人珍藏库9 小时前
[吾爱大神原创工具] FlowMouse - 心流鼠标手势 v1.0【Chrome浏览器插件】
前端·chrome·计算机外设
程序员杰哥9 小时前
接口测试之文件上传
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
oioihoii10 小时前
VS Code 1.107 更新:多智能体协同与开发体验升级
测试工具
软件测试大叔10 小时前
Playwright、Puppeteer、Selenium 自动化测试框架对比分析
测试工具
zhaotiannuo_199810 小时前
渗透测试之后端编程PHP
开发语言·chrome·php
2501_9240641111 小时前
2025年优测平台:接口最大并发量测试工具与实践方法
测试工具·性能优化·高并发系统·全链路压测·优测平台
2501_9240641113 小时前
2025年接口最大并发量测试工具对比与最佳实践方案
测试工具·压力测试·性能指标