2015-11-23 134 views
1

以下是我的Python代碼運行:硒(webdriver的)Python程序不要單和NOCONSOLE exe文件(pyinstaller)

## 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 exe文件(t.spec): Seems that IEDriver executable can't be recognized

當我改變OPTI:

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

它將按一下按鈕,運行時提示以下錯誤在spec文件的「console = 0」到「console = 1」中,單擊按鈕後可以運行IE。任何想法爲什麼設置「console = 0」時IE無法運行?

+0

您是否在運行之前嘗試在PATH環境變量中添加'C:\\ Program Files \\ Internet Explorer'? –

+0

是的,我的路徑如下,但仍不能解決此問題: PATH = C:\ ProgramData \ Oracle \ Java \ javapath; C:\ Windows \ system32; C:\ Windows; C:\ Windo ws \ System32 \ Wbem; C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \; C:\ Python34;「C:\ Pro gram Files \ Internet Explorer」 –

+0

在您最近的評論中,「pro」和「克」是你的評論中的錯字? –

回答

2

我想我通過在selenium包中修改服務類來解決這個問題。我不確定這是否是硒的缺陷(2.47.3)。當它調用subprocess.Popen函數時,原始代碼僅重定向stdoutstderr而不是stdin

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

要:

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

然後問題消失了

我從修改後的代碼。