小编典典

pyinstaller一个文件--no-console不起作用“致命错误”

selenium

我尝试使用pyinstaller的2个选项,一个带控制台,一个不带控制台。

我正在使用Windows 10,Python 3.5.4,Windows Chrome驱动程序2.33和Selnium 3.6.0和Pyinstaller
3.3。

在一个 没有 控制台失败:

这是 Test.py 的代码 __

#!python3
from selenium import webdriver

# Chrome Proxy options and disable the Yellow Bar about Chrome being controlled by Automated Software.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-logging")
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-default-apps")
chrome_options.add_argument("--disable-extensions")


# load google.com with the proxy settings and a logging path to a file
driver = webdriver.Chrome(r"D:\Selenium\chromedriver_win32\chromedriver.exe", chrome_options=chrome_options)

# maximise window
driver.maximize_window()

driver.get("https://www.google.com/mail")

这是具有完全相同代码的pyinstaller命令:

-使用控制台窗口也可以工作:

pyinstaller -F -i favicon.ico Test.py

-没有控制台窗口失败

pyinstaller -F --noconsole -i favicon.ico Test.py

我收到此错误:

*"Failed to execute script error"*

我不知道为什么。

谢谢


感谢您的回复,我查看了:

C:\Users\testuser\AppData\Local\Programs\Python\Python35\Lib\site-packages\selenium\webdriver\chrome\service.py

这里没有子流程。

我还检查了:

C:\Users\testuer\AppData\Local\Programs\Python\Python35\Lib\site-packages\selenium\webdriver\common\service.py

在第70行,我在stdin = self.log_file条目中添加了该条目,因为它丢失了

 try:
            cmd = [self.path]
            cmd.extend(self.command_line_args())
            self.process = subprocess.Popen(cmd, env=self.env,
                                            close_fds=platform.system() != 'Windows',
                                            stdin=self.log_file, stdout=self.log_file, stderr=self.log_file)

用pyinstaller重新创建:

pyinstaller -F --noconsole  -i favicon.ico Test.py

这创建了exe,但是现在控制台窗口出现了。

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


阅读 1301

收藏
2020-06-26

共1个答案

小编典典

经过一番搜索,我找到了完整的解决方案:首先转到-

C:\Python35\Lib\site-packages\selenium\webdriver\common\service.py

变更:

self.process = subprocess.Popen(cmd, env=self.env,
                                            close_fds=platform.system() != 'Windows',
                                            stdout=self.log_file, stderr=self.log_file)

至:

self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False, creationflags=0x08000000)

现在只需像这样构建您的app.py:

pyinstaller --noconsole --onefile  --noupx   Yourfile.py
2020-06-26