用Pyinstaller打包selenium的去掉chromedriver黑框

用Pyinstaller打包selenium的去掉chromedriver黑框

解决方案就是修改selenium包中的service.py(selenium->webdriver->common->service.py)源码。

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=platform.system() != 'Windows',
                                            stdout=self.log_file,
                                            stderr=self.log_file,
                                            stdin=PIPE)
        except TypeError:
            raise

添加 creationflags=134217728

self.process = subprocess.Popen(cmd, env=self.env,
                                            close_fds=platform.system() != 'Windows',
                                            stdout=self.log_file,
                                            stderr=self.log_file,
                                            stdin=PIPE,
                                            creationflags=134217728)

保存打包,黑框消失了

另一种方式

您必须编辑Selenium源代码才能实现此目的。
位于Python文件夹中的Lib \ site-packages \ selenium \ webdriver \ common \ service.py中。
通过以下方式添加创建标记来编辑**Start()**函数:
creationflags = CREATE_NO_WINDOW

编辑后的方法如下:

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=platform.system() != 'Windows',
                                        stdout=self.log_file, stderr=self.log_file, creationflags=CREATE_NO_WINDOW)
    except TypeError:
        raise

你不得不导入

from win32process import CREATE_NO_WINDOW

你可能感兴趣的:(python,selenium,去掉,黑框,pyinstaller)