Python Pyinstaller GUI Tkinter Selenium

标签 python selenium tkinter pyinstaller

在我来这里询问之前,我不知道如何创建一个可执行的python程序。值得庆幸的是,我很快就得到了答复,并且能够将我的脚本转换为可执行程序。该可执行文件完美运行,但仅在我的计算机上运行。 这是我收到的两个错误,我觉得我需要修改脚本才能找到 chrome 驱动程序,我不确定 Pyinstaller 在哪里保存了所有内容。

Exception in Tkinter callback
Traceback (most recent call last):
File "site-packages\selenium\webdriver\common\service.py", line 76, in start
File "subprocess.py", line 775, in __init__
File "subprocess.py", line 1178, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "tkinter\__init__.py", line 1705, in __call__
File "MarijuanaDoctors.py", line 25, in search
File "site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
File "site-packages\selenium\webdriver\common\service.py", line 83, in start
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' 
executable needs to be in PATH. Please see 
https://sites.google.com/a/chromium.org/chromedriver/home

最佳答案

您可以使用 Pyinstaller 将“chromedriver.exe”与脚本捆绑在一起,如下所示:

pyinstaller --add-binary="localpathtochromedriver;." myscript.py

这会将“chromedriver.exe”文件复制到与主 .exe 相同的文件夹中(或者在 pyinstaller 的单个文件选项的情况下,在使用 exe 程序时,此填充将被提取到临时文件夹中)。

在脚本中,您可以检查脚本是否正常运行或从捆绑(exe 文件)模式运行,并相应地选择 chromedriver.exe 的路径。(脚本中的此更改对于单文件/文件夹捆绑选项很常见pyinstaller)

import sys
if getattr(sys, 'frozen', False ):
    #Running from exe, so the path to exe is saved in sys._MEIPASS
    chrome_driver = os.path.join(sys._MEIPASS, "chromedriver.exe")
else:
    chrome_driver = 'localpathtochromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver)

您可以在文档 here 中阅读相关内容.

限制: .exe 的用户应在其系统上安装 Chrome,并且 Chrome 版本应与捆绑的 chromedriver 配合使用。

关于Python Pyinstaller GUI Tkinter Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54470616/

相关文章:

python - 将 JSON 写入文件而不将转义反斜杠写入文件?

java - 强制 Jenkins 返回稳定版本?

selenium - 方法executeScript(selenium web driver)不能定义全局变量供以后使用?

python - Tkinter:删除键盘双击预防

python - 同时在多个文本小部件上进行多项选择

python - 如何将Label的bg颜色设置为无颜色(默认颜色)?

python - 实例化时 Django REST 框架序列化器错误

python - 在 Django 中使用 manage.py 从 CLI 清除数据库的最简单方法是什么?

Python 正则表达式 : Fix one html close tag

java - 无法将 WebDriver 存储为全局变量并在同一 Swing 实例中多次执行测试