使用 selenium(webdriver) 的 Python 程序不能作为单个且无控制台的 exe 文件运行 (pyinstaller)

2023-12-06

以下是我的Python代码:

## t.py ##

from tkinter import messagebox
from tkinter import *
from selenium import webdriver

def clicked():
    iedriver = "C:\\Program Files\\Internet Explorer\\IEDriverServer.exe"
    try:        
        driver=webdriver.Ie(iedriver)        
    except Exception as e:        
        messagebox.showerror("Error",e)
    driver.get('www.baidu.com')  
Top=Tk()
Button(Top,text='Click Me',command=clicked).pack()
Top.mainloop()

这工作正常,但是当我使用 PyInstaller(t.spec) 将其转换为单个 .exe 文件时:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['D:\\program\\Python\\t.py'],
         pathex=['D:\\program\\Python'],
         binaries=None,
         datas=None,
         hiddenimports=[],
         hookspath=None,
         runtime_hooks=None,
         excludes=None,
         win_no_prefer_redirects=None,
         win_private_assemblies=None,
         cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)
exe = EXE(pyz,
      a.scripts,       
      a.binaries,
      a.zipfiles,
      a.datas,
      name='t',
      debug=False,
      strip=None,
      upx=False,
      console=0 , icon='D:\\program\\Python\\logo\\t.ico')

点击按钮运行时会提示如下错误:似乎无法识别 IEDriver 可执行文件

当我在spec文件中将选项“console=0”更改为“console=1”时,单击按钮后即可运行IE。知道为什么设置“console=0”时 IE 无法运行吗?


我想我通过修改解决了这个问题服务等级在硒包中。我不确定这是否是 selenium(2.47.3) 的错误。原来的代码只是重定向stdout and stderr但不是stdin当它调用时subprocess.Popen功能。

我修改了代码:

self.process = subprocess.Popen(cmd,
                stdout=PIPE, stderr=PIPE)

To:

self.process = subprocess.Popen(cmd, stdin=PIPE,
                stdout=PIPE, stderr=PIPE)

然后问题就消失了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 selenium(webdriver) 的 Python 程序不能作为单个且无控制台的 exe 文件运行 (pyinstaller) 的相关文章

随机推荐