最近用selenium做了一些微小的工作。自己用的时候当然是在命令行直接执行脚本就ok了,顺便还可以看看打在控制台的输出信息。使用pyinstaller打包成exe后,就发现了问题:虽然是窗口化运行,每次调用chrome的webdriver时,还是会弹出一个driver的控制台黑框框。虽然不妨碍使用,不过在闲下来后,我还是花了一些时间,研究了一下解决方案。
首先使用搜索引擎,搜了一会发现,有答案,但是都不是python语言的,python语言的答案还集中在headless这个浏览器参数上,根本解决不了问题。没办法只好自己动手了。
打开爱用的编辑器,在selenium源码里一路溯源过去,发现在site-packages\selenium\webdriver\common\service.py
文件,class Service
的start()
函数里,使用subprocess.Popen
启动了浏览器控制程序(chromedriver.exe)。就是这里了。
之后回到搜索引擎,继续搜索了一下,发现subprocess.Popen()可以通过指定一个windows专有的参数STARTUPINFO来达成不显示控制台窗口的目的。简单复制粘贴了一下代码后,做了一个最小测试例子:
# 测试startupinfo参数
IS_WINDOWS = 1
def subprocess_popen(cmd_list):
if IS_WINDOWS :
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
si.wShowWindow = subprocess.SW_HIDE
subprocess.Popen(cmd_list,
stdin=subprocess.PIPE,
startupinfo=si)
else:
subprocess.Popen(cmd_list,
stdin=subprocess.PIPE)
subprocess_popen(["chromedriver.exe"])
简单测试了一下,非常有效。剩下的就是怎么把这个startupinfo参数的传递缝合到selenium里面了...
于是花了十来分钟,整了一个自己的chromedriver。大部分代码都是从selenium里复制粘贴出来的,所以我也不知道是什么意思。 :P
# no_console_window_driver.py
# @author: kiri
import sys
import errno
import os
import subprocess
from subprocess import PIPE
import platform
import time
import warnings
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.remote_connection import ChromeRemoteConnection
class NoConsoleService(Service):
def start(self):
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
if 'win32' == sys.platform.lower():
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
si.wShowWindow = subprocess.SW_HIDE
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
startupinfo=si,
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE)
else:
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE)
except TypeError:
raise
except OSError as err:
if err.errno == errno.ENOENT:
raise WebDriverException(
"'%s' executable needs to be in PATH. %s" % (
os.path.basename(self.path), self.start_error_message)
)
elif err.errno == errno.EACCES:
raise WebDriverException(
"'%s' executable may have wrong permissions. %s" % (
os.path.basename(self.path), self.start_error_message)
)
else:
raise
except Exception as e:
raise WebDriverException(
"The executable %s needs to be available in the path. %s\n%s" %
(os.path.basename(self.path), self.start_error_message, str(e)))
count = 0
while True:
self.assert_process_still_running()
if self.is_connectable():
break
count += 1
time.sleep(1)
if count == 30:
raise WebDriverException("Can not connect to the Service %s" % self.path)
class NoConsoleChromeWebDriver(webdriver.Chrome):
"""modified base on "selenium\webdriver\common\service.py" """
def __init__(self, executable_path="chromedriver", port=0,
options=None, service_args=None,
desired_capabilities=None, service_log_path=None,
chrome_options=None, keep_alive=True):
if chrome_options:
warnings.warn('use options instead of chrome_options',
DeprecationWarning, stacklevel=2)
options = chrome_options
if options is None:
# desired_capabilities stays as passed in
if desired_capabilities is None:
desired_capabilities = self.create_options().to_capabilities()
else:
if desired_capabilities is None:
desired_capabilities = options.to_capabilities()
else:
desired_capabilities.update(options.to_capabilities())
self.service = NoConsoleService(
executable_path,
port=port,
service_args=service_args,
log_path=service_log_path)
self.service.start()
try:
RemoteWebDriver.__init__(
self,
command_executor=ChromeRemoteConnection(
remote_server_addr=self.service.service_url,
keep_alive=keep_alive),
desired_capabilities=desired_capabilities)
except Exception:
self.quit()
raise
self._is_remote = False
关于使用方法,简单替换一下就行。
# 以前的代码
from selenium import webdriver
driver = webdriver.Chrome(options=chrome_option)
# 现在的代码
from no_console_window_driver import NoConsoleChromeWebDriver
driver = NoConsoleChromeWebDriver(options=chrome_option)
强迫症治好了,搞定收工。这个代码是针对chrome driver的,如果要改别的如gekco driver,方法应该一样,就不再赘述了。
最后附上实验环境:
操作系统 | windows 10 2004 |
python版本 | 3.8.5 |
selenium版本 | 3.141.0 |
chromedriver版本 | ChromeDriver 2.41.578737 |
chrome版本 | 84.0.4147.105 |