简体   繁体   中英

Python program with selenium(webdriver) Don't Work as single and noconsole exe file (pyinstaller)

The following is my python code:

## 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):

# -*- 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

When I change the option "console=0" to "console=1" in the spec file, IE can be run after clicking the button. Any idea why IE can't be run when "console=0" is set?

I think I fixed this by modifying the Service class in selenium package. I'm not sure whether this is a bug for selenium(2.47.3). The original code is only redirecting stdout and stderr but not stdin when it calls subprocess.Popen function.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM