![](/img/trans.png)
[英]Standalone app that incorporates python, selenium and chrome webdriver
[英]How to make a standalone Python executable with selenium/chrome webdriver on MacOS using PyInstaller
我正在嘗試制作一個使用 selenium 和 webdriver 的網絡抓取腳本的獨立可執行文件,並且我希望能夠將文件共享給其他用戶,而無需他們手動安裝 chromedriver 並指定其路徑。
當我在同一目錄中使用 chromedriver 運行可執行文件時,我在終端中得到以下 output :
Traceback (most recent call last):
File "site-packages/selenium/webdriver/common/service.py", line 76, in start
File "subprocess.py", line 707, in __init__
File "subprocess.py", line 1326, in _execute_child
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "excelRW.py", line 336, in <module>
File "site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
File "site-packages/selenium/webdriver/common/service.py", line 83, in start
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
在我的腳本中,這就是我初始化 webdriver 的內容:
driver = webdriver.Chrome()
我確實在這里找到了類似的問題/答案: Create a Python executable with chromedriver & Selenium
但我不確定如何在 MacOS 上進行這項工作; 我在這里嘗試了這個規范文件,但是當我運行 /dist/excelRW.py 時,它給了我與上面相同的 output:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['excelRW.py'],
pathex=['/Users/graememjehovich/Desktop/House Districting Script'],
binaries=[('/Users/graememjehovich/Desktop/House Districting Script/chromedriver', '**./selenium/webdriver**')],
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='excelRW',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='**excelRW.py**')
也許使用WebDriverManager ,它會在運行時下載最新的 webdriver,所以你根本不需要發布它。 這也解決了您的用戶在升級 Chrome 版本時可能遇到的問題,因為 Chrome 版本和驅動程序需要匹配。 因此,如果您在安裝程序中對驅動程序版本進行硬編碼,則抓取工具將在用戶升級瀏覽器后停止工作,這在 Chrome 中會自動發生。
我這樣使用它:
from webdriver_manager.chrome import ChromeDriverManager
webdriver.Chrome(ChromeDriverManager().install())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.