分享

selenium+python 去除启动的黑色cmd窗口

 山泉的往事 2018-04-04

其实 selenium启动窗口的时候就是 使用了subprocess.Popen 启动的驱动程序的,只要在启动的时候加上启动不显示窗口的参数即可。

修改代码   位于 D:\Python35\Lib\site-packages\selenium\webdriver\common\service.py    主要是 Service类的start函数  

  1. def start(self):  
  2.        """ 
  3.        Starts the Service. 
  4.  
  5.        :Exceptions: 
  6.         - WebDriverException : Raised either when it can't start the service 
  7.           or when it can't connect to the service 
  8.        """  
  9.        try:  
  10.            cmd = [self.path]  
  11.            cmd.extend(self.command_line_args())  
  12.            if 'win32' in str(sys.platform).lower():   ### 这里判断是否是windows平台  
  13.                ###   在windows平台上就隐藏窗口  
  14.   
  15.                startupinfo = subprocess.STARTUPINFO()  
  16.                startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW  
  17.                startupinfo.wShowWindow = subprocess.SW_HIDE  
  18.            else:  
  19.                startupinfo = None  
  20.   
  21.   
  22.            self.process = subprocess.Popen(cmd, env=self.env,  
  23.                                            close_fds=platform.system() != 'Windows',  
  24.                                            stdout=self.log_file, stderr=self.log_file,startupinfo=startupinfo)   ### 启动驱动  
  25.   
  26.   
  27.   
  28.            self.PID = self.process.pid  ### 将cmd窗口的进程pid 保留   因为 窗口被隐藏了  所以在后续程序中必须考虑主控进程结束的时候必须结束掉 驱动cmd窗口进程  
  29.        except TypeError:  
  30.            raise  
  31.        except OSError as err:  
  32.            if err.errno == errno.ENOENT:  
  33.                raise WebDriverException(  
  34.                    "'%s' executable needs to be in PATH. %s" % (  
  35.                        os.path.basename(self.path), self.start_error_message)  
  36.                )  
  37.            elif err.errno == errno.EACCES:  
  38.                raise WebDriverException(  
  39.                    "'%s' executable may have wrong permissions. %s" % (  
  40.                        os.path.basename(self.path), self.start_error_message)  
  41.                )  
  42.            else:  
  43.                raise  
  44.        except Exception as e:  
  45.            raise WebDriverException(  
  46.                "The executable %s needs to be available in the path. %s\n%s" %  
  47.                (os.path.basename(self.path), self.start_error_message, str(e)))  
  48.        count = 0  
  49.        while True:  
  50.            self.assert_process_still_running()  
  51.            if self.is_connectable():  
  52.                break  
  53.            count += 1  
  54.            time.sleep(1)  
  55.            if count == 30:  
  56.                raise WebDriverException("Can not connect to the Service %s" % self.path)  
注意  在前面先导入 sys包     

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多