selenium、chromedriver_autoinstaller 和 pyinstaller 在一个文件中

Tyl*_*son 2 python selenium pyinstaller selenium-chromedriver

我正在尝试使用 pyinstaller 创建一个 onefile .exe,它将利用 chromedriver_autoinstaller 以便 chromedriver 始终是最新的。该代码在 IDE 中运行时工作正常,但一旦我使用 pyinstaller 创建 .exe,它就会抛出“chrome_autoinstaller 的特定问题”消息。由于 chromedriver_autoinstaller 不起作用,因此程序无法找到 chromedriver。该程序确实可以通过直接引用 chromedriver 文件路径在没有自动安装程序的 .exe 中运行,但如果可能的话,我更愿意使用这个包。

class LoginPCC:
    def __init__(self):  # create an instance of this class. Begins by logging in
        try:
            chrome_options = webdriver.ChromeOptions()
            settings = {
                "recentDestinations": [{
                    "id": "Save as PDF",
                    "origin": "local",
                    "account": "",
                }],
                "selectedDestinationId": "Save as PDF",
                "version": 2
            }
            prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(settings), 
            "plugins.always_open_pdf_externally": True}
            chrome_options.add_experimental_option('prefs', prefs)
            chrome_options.add_argument('--kiosk-printing')
            try:
                chromedriver_autoinstaller.install()
            except:
                print('Specific issue with chrome_autoinstaller')
            try:
                self.driver = webdriver.Chrome(options=chrome_options)
            except:
                print('Cannot find chromedriver')
Run Code Online (Sandbox Code Playgroud)

python 3.7 pyinstaller 4.0.dev selenium 4.141.0 chromedriver-autoinstaller 0.2

小智 5

以下内容对我有用:

chromedriver_autoinstaller.install(cwd=True)
Run Code Online (Sandbox Code Playgroud)

当您第一次启动 EXE 时,将在您放置 EXE 的文件夹中创建一个新文件夹。这是 chromedriver 的存储位置。这不是最漂亮的解决方案,但它确实有效。