Selenium启动谷歌浏览器便携版时如何隐藏命令提示符窗口?
解决Selenium启动便携版Chrome时弹出命令提示符窗口的问题
以下是几种可行的解决方法:
方法一:修正路径转义并增强参数抑制
先修复代码中可能存在的路径转义问题,同时添加抑制日志的参数,减少控制台窗口的触发概率:
from selenium import webdriver from selenium.webdriver.chrome.service import Service import subprocess import os current_directory = os.path.dirname(os.path.abspath(__file__)) # 使用原始字符串避免路径转义错误 driver_path = os.path.join(current_directory, "chromedriver.exe") chrome_path = os.path.join(current_directory, r"GoogleChromePortable_114.0.5735.91\GoogleChromePortable.exe") profile_path = os.path.join(current_directory, r"GoogleChromePortable_114.0.5735.91\Data\profile\Default") chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--remote-debugging-port=9222") chrome_options.add_argument("--user-data-dir=" + profile_path) # 添加抑制日志参数,减少控制台输出触发 chrome_options.add_argument("--disable-logging") chrome_options.add_argument("--log-level=3") chrome_options.add_argument("--silent") chrome_service = Service(driver_path) chrome_service.creation_flags = subprocess.CREATE_NO_WINDOW chrome_options.binary_location = chrome_path browser = webdriver.Chrome(options=chrome_options, service=chrome_service)
方法二:手动隐藏启动Chrome再连接调试端口
如果方法一无效,说明是便携版Chrome自身启动时弹出CMD窗口,可以通过subprocess先以隐藏窗口方式启动Chrome,再让Selenium连接到已启动的实例:
from selenium import webdriver from selenium.webdriver.chrome.service import Service import subprocess import os import time current_directory = os.path.dirname(os.path.abspath(__file__)) driver_path = os.path.join(current_directory, "chromedriver.exe") chrome_path = os.path.join(current_directory, r"GoogleChromePortable_114.0.5735.91\GoogleChromePortable.exe") profile_path = os.path.join(current_directory, r"GoogleChromePortable_114.0.5735.91\Data\profile\Default") # 隐藏启动便携版Chrome startup_info = subprocess.STARTUPINFO() startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW startup_info.wShowWindow = subprocess.SW_HIDE subprocess.Popen( [chrome_path, "--remote-debugging-port=9222", f"--user-data-dir={profile_path}"], startupinfo=startup_info, creationflags=subprocess.CREATE_NO_WINDOW ) time.sleep(2) # 等待Chrome启动完成 # 连接到已启动的Chrome实例 chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") chrome_service = Service(driver_path) chrome_service.creation_flags = subprocess.CREATE_NO_WINDOW browser = webdriver.Chrome(options=chrome_options, service=chrome_service)
方法三:用VBS脚本替换批处理启动(适用于便携版为批封装的情况)
如果便携版Chrome的启动文件是.bat或.cmd,可以创建VBS脚本启动以避免CMD窗口:
- 创建
launch_chrome.vbs文件,内容如下(替换为你的实际路径):
Set objShell = CreateObject("WScript.Shell") objShell.Run """你的便携版Chrome.exe完整路径"" --remote-debugging-port=9222 --user-data-dir=""你的配置文件路径""", 0, False
- 修改代码中启动路径为该VBS脚本:
from selenium import webdriver from selenium.webdriver.chrome.service import Service import subprocess import os current_directory = os.path.dirname(os.path.abspath(__file__)) driver_path = os.path.join(current_directory, "chromedriver.exe") # 指向VBS脚本,通过wscript启动 chrome_path = "wscript.exe" vbs_path = os.path.join(current_directory, "launch_chrome.vbs") chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--remote-debugging-port=9222") chrome_options.binary_location = chrome_path chrome_options.add_argument(vbs_path) chrome_service = Service(driver_path) chrome_service.creation_flags = subprocess.CREATE_NO_WINDOW browser = webdriver.Chrome(options=chrome_options, service=chrome_service)
内容的提问来源于stack exchange,提问作者William
相关产品推荐
相关产品推荐

