6 python selenium pyinstaller python-3.x selenium-webdriver
我正在尝试在 pyinstaller 的可执行文件中添加 Chromedriver。虽然这是可能的,但当我尝试在另一台计算机上运行它时,我似乎收到以下错误消息。
我已经尝试了一些职位,包括本的一个,但不幸的是,这并没有提供预期的效果。最好的情况是,当 chrome exe 位于同一个文件夹中时,我可以在自己的计算机上运行它,这没有帮助。
代码 1:
主文件
from selenium import webdriver
driver = webdriver.Chrome()
Run Code Online (Sandbox Code Playgroud)
在另一台电脑上运行时得到的结果:
错误 1:
找不到 Chrome 路径
C:\Users\Aperture Science\Desktop\1>123.exe
Traceback (most recent call last):
File "site-packages\selenium\webdriver\common\service.py", line 74, in start
File "subprocess.py", line 709, in __init__
File "subprocess.py", line 997, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 42, in <module>
File "main.py", line 33, in main
File "site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
File "site-packages\selenium\webdriver\common\service.py", line 81, 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
[2228] Failed to execute script main
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
我从提供的链接中得到的信息:
代码 2:
from selenium import webdriver
import os, sys, inspect
current_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe() ))[0]))
chromedriver = os.path.join(current_folder,"chromedriver.exe")
driver = webdriver.Chrome(executable_path = chromedriver)
driver.get("http://www.imdb.com/")
Run Code Online (Sandbox Code Playgroud)
需要设置路径中的 Chrome exe,捆绑的 chrome 未读取。所以打包的 chrome 不能按预期工作。
Flo*_* B. 11
用于--add-binary
在应用程序中捆绑驱动程序:
pyinstaller -F --add-binary "C:\drivers\chromedriver.exe";"." script.py
Run Code Online (Sandbox Code Playgroud)
并用于sys._MEIPASS
获取提取驱动程序的文件夹:
import sys, os, time
from selenium import webdriver
if __name__ == "__main__":
if getattr(sys, 'frozen', False):
# executed as a bundled exe, the driver is in the extracted folder
chromedriver_path = os.path.join(sys._MEIPASS, "chromedriver.exe")
driver = webdriver.Chrome(chromedriver_path)
else:
# executed as a simple script, the driver should be in `PATH`
driver = webdriver.Chrome()
driver.get("https://stackoverflow.com")
time.sleep(5)
driver.quit()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5205 次 |
最近记录: |