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

后记

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

相关推荐
SQDN12 小时前
Chatbox 1.22.1 获取不到模型?先验 /models,再对齐精确 model ID
人工智能·测试工具·机器学习·chatgpt·json
Luminbox紫创测控15 小时前
可调UV宽光谱高功率的LED太阳能模拟器
人工智能·测试工具·汽车·安全性测试·uv·测试标准
Li Ming&16 小时前
图吧工具箱下载安装《保姆级日常使用教程》
测试工具·电脑
测试秃头怪17 小时前
postman接口测试详解
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
m0_7263658318 小时前
# Chrome纸牌游戏预测工具开发实战:Canvas图表识别、OCR与实时状态监控
chrome·python·游戏·贪心算法·ocr·线性回归·哈希算法
CYB35322 天前
KEYSIGHT是德科技 E8362C PNA系列20 GHz高性能微波矢量网络分析仪
功能测试·科技·测试工具
wardenlzr2 天前
VSCode 运行 Live Server 提示「Windows 找不到文件 Chrome」问题全解析
chrome·windows·vscode
CYB35322 天前
KEYSIGHT N9020A是德科技 高性能MXA信号分析仪
功能测试·测试工具
祉猷并茂,雯华若锦2 天前
Selenium+Pytest自动化测试框架实战
selenium·测试工具·pytest