简体   繁体   English

带有selenium(webdriver)的Python程序不能作为单个noconsole exe文件工作(pyinstaller)

[英]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. 原始代码仅重定向stdoutstderr ,而不重定向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.

相关问题 程序无法作为单个noconsole exe文件运行(Pyinstaller) - Program doesn't work as single and noconsole exe file (Pyinstaller) 在 pyinstaller 中以窗口模式导出到 EXE 后,Selenium 不起作用 - Selenium don't work after exporting to EXE in windowed mode in pyinstaller 带有--noconsole的Windows上的pyinstaller无法正常工作 - pyinstaller on Windows with --noconsole simply won't work 将 selenium 与 pyinstaller 一起使用不起作用? - Using selenium with pyinstaller don't work? Pyinstaller --windowed或--noconsole .exe不允许chromedriver打开 - Pyinstaller --windowed or --noconsole .exe not allowing chromedriver to open Python(使用 Tkinter 和 Selenium Webdriver,使用 pyinstaller 创建)应用程序无法在其他电脑上运行 - Python (with Tkinter and Selenium Webdriver, created with pyinstaller) app doesn't work on other pc's Python - Pyinstaller - 导入 selenium webdriver - Python - Pyinstaller - Import selenium webdriver pyinstaller2.0 --noconsole在我单击它时不会运行我的exe - pyinstaller2.0 --noconsole won't run my exe when I click on it 如何在不使用pyinstaller的情况下为带有spacy的python程序创建exe文件? - How to create a exe file for python program with spacy without using pyinstaller? 如何使用pyinstaller将多个python文件编译成单个.exe文件 - How to compile multiple python files into single .exe file using pyinstaller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM