使用pyinstaller打包后的selenium项目设置了-w不显示窗口的设置,但还是显示窗口的解决办法
本人使用版本是python3.10
在原有的基础上导入包
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
pycharm上面可能搜不到【webdriver_manager】这个包
建议直接使用 pip install webdriver_manager,直接下载
通过编写类来实现service的创建
class MyService(Service):
def __init__(self, executable_path: str,
port: int = 0, service_args=None,
log_path: str = None, env: dict = None):
super(Service, self).__init__(
executable_path,
port,
service_args,
log_path,
env,
"Please see https://chromedriver.chromium.org/home")
self.creation_flags = 134217728
# 设置chrome浏览器无界面模式
options = Options()
options.add_argument('--headless')
options.add_experimental_option("excludeSwitches", ['enable-logging'])
driver = webdriver.Chrome(chrome_options=options,service=MyService(ChromeDriverManager().install()))
最后再使用打包命令即可
pyinstaller -F -w xxx.py