繁体   English   中英

在 Mac 上通过 chromedriver 和 pyinstaller 使用 selenium

[英]Use selenium with chromedriver and pyinstaller on Mac

我创建了应用程序来像下面的代码一样打开浏览器,它在 cmd 行中运行良好。
但是,当我用 pyinstaller 打包它时,它不起作用..

有什么问题? 我将感谢您对这种情况的帮助。

# main.py
# -*- coding: utf-8 -*-
import os

from selenium import webdriver

if __name__ == '__main__':
    PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
    DRIVER_BIN = os.path.join(PROJECT_ROOT, "chromedriver")

    browser = webdriver.Chrome(executable_path=DRIVER_BIN)
    browser.get('https://google.com/')

这是 .spec 文件。

# -*- mode: python -*-
import os

project_path = os.path.abspath(os.path.curdir)

block_cipher = None

a = Analysis(['main.py'],
             pathex=[project_path],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='test',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=False)

app = BUNDLE(exe,
         name='test.app',
         icon=None,
         bundle_identifier=None)

文件树就是这样。

dist/  
      test.app  
      test  
      chromedriver <- add after packaged

错误可能是这个。

Jun 26 14:42:01 MacBookPro ctkahp[67399]: objc[67399]: Class TKTokenRefCtkd is implemented in both /System/Library/Frameworks/Security.framework/Versions/A/Sec    urity (0x7fff8a3cf0a0) and /System/Library/Frameworks/CryptoTokenKit.framework/ctkahp.bundle/Contents/MacOS/ctkahp (0x10ecbc760). One of the two will be used.     Which one is undefined.
Jun 26 14:42:16 MacBookPro com.apple.xpc.launchd[1] (highlow.23356[67408]): Service exited with abnormal code: 255

我找到了解决方案。
我试图将完整路径设置为可执行文件路径。 然后它工作正常。

但我不明白为什么这是问题。

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "chromedriver")

browser = webdriver.Chrome(executable_path=DRIVER_BIN)

还有这个。

browser = webdriver.Chrome(executable_path="./chromedriver")

.app 文件在哪里看?

命令执行的测试脚本的路径与“bundle”不同。

你可以试试这个
主文件

import os
import sys

if __name__ == "__main__":
  if getattr(sys, 'frozen', False): 
    chromedriver_path = os.path.join(sys._MEIPASS, "chromedriver")
    driver = webdriver.Chrome(chromedriver_path)
  else:
    driver = webdriver.Chrome()

主要规格

project_path = os.path.abspath(os.path.curdir)
a = Analysis(['main.py'],
             pathex=[project_path],
             binaries=[('/Users/xxx/xxx/chromedriver', './')],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

(参考: https : //github.com/pyinstaller/pyinstaller/issues/1726
(参考:使用Chromedriver 在另一台电脑上运行 pyinstaller

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM