其实 selenium 启动窗口的时候就是 使用了 subprocess.Popen 启动的驱动程序的, 只要在启动的时候加上启动不显示窗口的参数即可
下面魔改开始 O(_)O 哈哈~
修改代码 位于 D:\Python35\Lib\site-packages\selenium\webdriver\common\service.py 主要是 Service 类的 start 函数
- [python] view plain copy
- print?
- 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())
- if 'win32' in str(sys.platform).lower(): ### 这里判断是否是 windows 平台
- ### 在 windows 平台上就隐藏窗口
- startupinfo = subprocess.STARTUPINFO()
- startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
- startupinfo.wShowWindow = subprocess.SW_HIDE
- else:
- startupinfo = None
- self.process = subprocess.Popen(cmd, env=self.env,
- close_fds=platform.system() != 'Windows',
- stdout=self.log_file, stderr=self.log_file,startupinfo=startupinfo) ### 启动驱动
- self.PID = self.process.pid ### 将 cmd 窗口的进程 pid 保留 因为 窗口被隐藏了 所以在后续程序中必须考虑主控进程结束的时候必须结束掉 驱动 cmd 窗口进程
- 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)
注意 在前面先导入 sys 包
因为隐藏了驱动 cmd 窗口 所以 结束程序的时候 一定要做杀死驱动 cmd 窗口的动作哦 !O(_)O!!
来源: http://www.92to.com/bangong/2018/04-04/33517048.html