python+selenium如何隐藏chromdriver.exe窗口?

初学python,利用selenium写了一个简单的自动登录校园网的脚本,chrome浏览器驱动已经采用了无头模式,问题在于运行过程中,chromedriver的窗口一直显示,不方便进行其他的操作,在网上搜了半天,也没能找到解决办法,所以特来问询。
需要隐藏的窗口如下:图片说明

用c++写一个程序,代码如下:

#include <stdio.h>
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
    int n = 0;
    HWND h = FindWindow(NULL, _T("C:\\WINDOWS\\py.exe")); 
    ShowWindow(h, n); // n=0 隐藏,n=1显示
    return 0;
}

放在你脚本里调用

运行效果:https://ask.csdn.net/questions/700661

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
options = webdriver.ChromeOptions()
prefs = {"download.default_directory": r"C:\New_Download"}
options.add_experimental_option("prefs", prefs)
print(options.experimental_options)

chromeDriverPath = r'C:\drivers\chromedriver.exe'
driver = webdriver.Chrome(chromeDriverPath,chrome_options=options)
driver.get("http://google.com")
driver.set_window_position(0, 0)
driver.set_window_size(0, 0)
请问楼主解决了吗
我尝试了上面方法,好像不行

https://stackoverflow.com/questions/33983860/hide-chromedriver-console-in-python

这是第一位答主的链接,

在python安装目录

C:\Program Files\Python37\Lib\site-packages\selenium\webdriver\common\service.py

导入命令附近添加如下

from subprocess import CREATE_NO_WINDOW

对service.py start修改,

            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)

干净了,不弹窗不提示了。pyinstaller打包exe也舒服了

 

修改一下selenium的源码。参考:https://stackoverflow.com/questions/33983860/hide-chromedriver-console-in-python

 from selenium import webdriver
from selenium.webdriver.chrome.options import Options 
from time import sleep
options = webdriver.ChromeOptions()
prefs = {"download.default_directory":  r"C:\New_Download"}
options.add_experimental_option("prefs", prefs)
print(options.experimental_options)

chromeDriverPath = r'C:\drivers\chromedriver.exe'
driver = webdriver.Chrome(chromeDriverPath,chrome_options=options)
driver.get("http://google.com")
driver.set_window_position(0, 0)
driver.set_window_size(0, 0)