【方法一】亲测有效
问题:如何关闭selenium开发中的chromedriver.exe 或 geckodriver.exe 的console命令行窗
采用selenium操作浏览器执行自动化操作的场景时,在使用 pyinstaller 打包成exe文件后,会有chromedriver.exe 或 geckodriver.exe 的console命令行窗口。
我们打包成exe文件一般是要分发到客户电脑上,出现这个窗体不太友好,我们需要把它隐藏掉:
先安装pywin32 安装命令:pip install pywin32
找到python安装目录,比如:
D:\Python\Python37\Lib\site-packages\selenium\webdriver\common\service.py
==》位置约: 72行
修改这个文件,先在顶部导入依赖包:from win32process import CREATE_NO_WINDOW
然后找到def start(self) 函数,里面的 subprocess.Popen增加一个参数:creationflags=CREATE_NO_WINDOW
最终改成:
```
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)
```
也可以直接使用creationflags=0x08000000这样的值。
重新打包 pyinstaller -F -w main.py 就可以了。如果还有命令行窗体出现看下面 关闭cmd控制台中的日志打印
转载自:
https://blog.csdn.net/xinxianren007/article/details/108031372
https://stackoverflow.com/questions/33983860/hide-chromedriver-console-in-python
http://www.piaoyi.org/python/python-dev-tips.html
关闭cmd控制台中的日志打印
要关闭这个打印需要在创建驱动对象之前设置以下参数:
```
c_service = Service(r'chromedriver.exe')#驱动路径 我为了方便直接放在代码根目录下
options = webdriver.ChromeOptions()#加启动设置
options.add_experimental_option('excludeSwitches', ['enable-logging'])#禁止打印日志
wd = webdriver.Chrome(options=options)
```
下面是关闭其他功能的一些参数设置
```
# 加启动配置
chrome_options = webdriver.ChromeOptions()
# 打开chrome浏览器
# 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
#chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])#禁止打印日志
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])#跟上面只能选一个
chrome_options.add_argument('--headless') # 无头模式
chrome_options.add_argument('--disable-gpu') # 上面代码就是为了将Chrome不弹出界面
chrome_options.add_argument('--start-maximized')#最大化
chrome_options.add_argument('--incognito')#无痕隐身模式
chrome_options.add_argument("disable-cache")#禁用缓存
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument('log-level=3')#INFO = 0 WARNING = 1 LOG_ERROR = 2 LOG_FATAL = 3 default is 0
browser = webdriver.Chrome(chrome_options=chrome_options)
```
转载自:
https://www.jianshu.com/p/e80e95165726