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

后记

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

相关推荐
秃了也弱了。9 小时前
WireShark:非常好用的网络抓包工具
网络·测试工具·wireshark
shuangrenlong10 小时前
ubuntu更新chrome版本
linux·chrome·ubuntu
敲上瘾11 小时前
Linux系统cgroups资源精细化控制基础
linux·测试工具·docker·压力测试·cgroups
gc_229916 小时前
使用HtmlAgilityPack+PuppeteerSharp+iText7抓取Selenium帮助文档
selenium·itext7·htmlagilitypack·puppeteersharp
天才测试猿17 小时前
常见的Jmeter压测问题
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·压力测试
是晓晓吖20 小时前
为什么在Tab中取不到content.js给window设置的变量/函数?
前端·chrome
发如雪-ty1 天前
Bash常用操作总结
前端·chrome
lpfasd1232 天前
开发Chrome/Edge插件基本流程
前端·chrome·edge
森之鸟2 天前
Mac chrome浏览器下载DevEco Studio 6.0.0 Beta2失败
前端·chrome·macos
测试开发Kevin2 天前
详解grafana k6 中stage的核心概念与作用
测试工具·压力测试·grafana