【Python】selenium 打包后报错解决
前言
最近,我着手开发了一个用于Web自动化测试的脚本,其目标是在单一机器上实现自动化流程:自动启动Chrome浏览器、自动登录以及自动填写表单。然而,在尝试使用pyinstaller
进行打包后,我遇到了无法运行的问题。
问题
在尝试执行打包后的应用程序app_selenium.exe
时,我遇到了一个错误,错误信息如下所示:
PS C:\Users\fg\Desktop> .\app_selenium.exe
Traceback (most recent call last):
File "app_selenium.py", line 2, in <module>
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "selenium\webdriver\__init__.py", line 20, in <module>
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "selenium\webdriver\chrome\webdriver.py", line 18, in <module>
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "selenium\webdriver\chromium\webdriver.py", line 18, in <module>
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "selenium\webdriver\chromium\remote_connection.py", line 18, in <module>
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "selenium\webdriver\remote\remote_connection.py", line 131, in <module>
File "selenium\webdriver\remote\remote_connection.py", line 140, in RemoteConnection
File "certifi\core.py", line 72, in where
File "contextlib.py", line 135, in __enter__
File "importlib\_common.py", line 87, in _tempfile
File "tempfile.py", line 337, in mkstemp
File "tempfile.py", line 249, in _mkstemp_inner
TypeError: can only concatenate str (not "method") to str
[6896] Failed to execute script 'app_selenium' due to unhandled exception!
PS C:\Users\fg\Desktop>
解决
经过深入研究,我发现问题确实出在脚本本身。当直接运行脚本时一切正常,但在打包后执行时却出现了问题。原因在于打包的脚本需要移除首行的注释。以下是我的脚本示例:
# 注释
from selenium.webdriver.common.by import By
from selenium import webdriver
我将#注释
去掉后,马上就编译,之后便可以运行了。
我的打包命令
pyinstaller -F .\app_selenium.py --exclude-module _bootlocale
本文来自:【Python】selenium 打包后报错解决-小码农,转载请保留本条链接,感谢!
温馨提示:
本文最后更新于 2024年07月18日,已超过 84 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
正文到此结束
- 本文标签: python pyinstall 打包
- 本文链接: https://djc8.cn/archives/【python】sem.html
- 版权声明: 本文由小码农原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权