[英]Python program with selenium(webdriver) Don't Work as single and noconsole exe file (pyinstaller)
The following is my python code: 以下是我的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()
This works fine, but when I convert this to a single .exe file with PyInstaller(t.spec): 这可以正常工作,但是当我使用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')
It will prompt the following error when clicking the button to run: Seems that IEDriver executable can't be recognized 单击要运行的按钮时,它将提示以下错误: 似乎无法识别IEDriver可执行文件
When I change the option "console=0" to "console=1" in the spec file, IE can be run after clicking the button. 当我在规格文件中将选项“ console = 0”更改为“ console = 1”时,单击该按钮后即可运行IE。 Any idea why IE can't be run when "console=0" is set? 知道为什么在设置“ console = 0”时无法运行IE吗?
I think I fixed this by modifying the Service class in selenium package. 我想我通过修改硒包中的Service类来解决此问题。 I'm not sure whether this is a bug for selenium(2.47.3). 我不确定这是否是硒(2.47.3)的错误。 The original code is only redirecting stdout
and stderr
but not stdin
when it calls subprocess.Popen
function. 原始代码仅重定向stdout
和stderr
,而不重定向stdin
当它调用subprocess.Popen
函数时。
I modified the code from: 我修改了以下代码:
self.process = subprocess.Popen(cmd,
stdout=PIPE, stderr=PIPE)
To: 至:
self.process = subprocess.Popen(cmd, stdin=PIPE,
stdout=PIPE, stderr=PIPE)
Then the issue is gone. 然后问题解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.