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

后记

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

相关推荐
天才测试猿10 小时前
WebUI自动化测试:POM设计模式全解析
自动化测试·软件测试·python·selenium·测试工具·设计模式·测试用例
科技百宝箱12 小时前
02-如何使用Chrome工具排查内存泄露问题
前端·chrome
郁大锤15 小时前
在 Windows 下安装与快速上手 Wireshark(抓包工具)
windows·测试工具·wireshark
2501_9159090619 小时前
tcpdump 抓包数据分析实战,命令、过滤、常见故障定位与真机补充流程
网络·测试工具·ios·小程序·uni-app·iphone·tcpdump
00后程序员张1 天前
tcpdump 抓包分析,命令、过滤技巧、常见症状定位与移动真机补充方案
网络·测试工具·ios·小程序·uni-app·iphone·tcpdump
天生励志1231 天前
【学习总结】AI接口测试-零基础从接口概念到客达天下系统Apifox+DeepSeek接口测试实战全流程
测试工具·apifox
晚枫~1 天前
零基础快速上手Playwright自动化测试
javascript·python·测试工具·c#·自动化
西瓜树枝1 天前
Chrome 扩展开发从入门到实践:以 Cookie 跨页提取工具为例,拆解核心模块与交互逻辑
前端·javascript·chrome
卓码软件测评1 天前
第三方软件测评机构:【Locust的性能测试和负载测试】
测试工具·测试用例·压力测试·可用性测试
Harriet嘉1 天前
解决Chrome 140以上版本“此扩展程序不再受支持,因此已停用”问题 axure插件安装问题
前端·chrome