问题描述
用Pyinstaller打包后,运行exe文件时总是出现driver控制台窗口,如下图
希望程序运行时,隐藏driver控制台窗口
下面针对以上问题给出解决方案
首先找到selenium包源码文件service.py中的start函数,文件路径如下:
Lib\site-packages\selenium\webdriver\common\service.py
找到文件中start函数,具体代码如下:
def start(self):
"""
Starts the Service.
:Exceptions:
- WebDriverException : Raised either when it can't start the service
or when it can't connect to the service
"""
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE,
creationflags=self.creationflags)
except TypeError:
raise
方案①
修改start函数参数creationflags=self.creationflags为creationflags=134217728,具体代码如下:
def start(self):
"""
Starts the Service.
:Exceptions:
- WebDriverException : Raised either when it can't start the service
or when it can't connect to the service
"""
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE,
creationflags=134217728)
except TypeError:
raise
方案②
在services.py开头添加一行代码文章来源:https://www.toymoban.com/news/detail-529696.html
from win32process import CREATE_NO_WINDOW
然后修改参数creationflags=self.creationflags为creationflags=CREATE_NO_WINDOW,具体代码如下:文章来源地址https://www.toymoban.com/news/detail-529696.html
def start(self):
"""
Starts the Service.
:Exceptions:
- WebDriverException : Raised either when it can't start the service
or when it can't connect to the service
"""
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE,
creationflags=CREATE_NO_WINDOW)
except TypeError:
raise
实验环境
以上实验环境:
python 3.7
pyinstaller 5.1
重要声明:
python 3.9不适用以上方法
python 3.8(小编未尝试)
python 3.7及以下应该没问题
#用心交流,共获提升,转载请注明出处。
到了这里,关于完美解决Pyinstaller打包selenium去除driver黑框问题。的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!