selenium 去除cmd窗口

解决启动浏览器时win打开cmd窗口

修复代码路径: (python环境)/Lib/site-packages/selenium/webdriver/commom/service.py

替换start

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())
            # self.process = subprocess.Popen(cmd, env=self.env,
            #                                 close_fds=system() != 'Windows',
            #                                 stdout=self.log_file,
            #                                 stderr=self.log_file,
            #                                 stdin=PIPE,
            #                                 creationflags=134217728)
            # self.PID = self.process.pid
            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=system() != 'Windows',
                                            stdout=self.log_file,
                                            stderr=self.log_file,
                                            stdin=PIPE,
                                            creationflags=134217728,
                                            startupinfo=startupinfo) ### 启动驱动
            self.PID = self.process.pid ### 将cmd窗口的进程pid 保留 因为 窗口被隐藏了 所以在后续程序中必须考虑主控进程结束的时候必须结束掉 驱动cmd窗口进程
        except TypeError:
            raise
        except OSError as err:
            if err.errno == errno.ENOENT:
                raise WebDriverException(
                    "'{}' executable needs to be in PATH. {}".format(
                        os.path.basename(self.path), self.start_error_message)
                )
            elif err.errno == errno.EACCES:
                raise WebDriverException(
                    "'{}' executable may have wrong permissions. {}".format(
                        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
            sleep(0.5)
            if count == 60:
                raise WebDriverException("Can not connect to the Service %s" % self.path)
        # if self.PID:
        #     os.kill(self.PID, signal.SIGKILL)```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值