我试图在pyinstaller中的可执行文件中添加Chromedriver.虽然这是可能的,但在尝试在另一台计算机上运行时,我似乎得到以下错误消息.
我已经尝试了很多帖子,包括这个one,但不幸的是,这没有提供预期的结果.最好的情况是我可以在我自己的计算机上运行它,因为chrome exe在同一个文件夹中是无益的.
代码1:
Main.py
from selenium import webdriver
driver = webdriver.Chrome()
在另一台电脑上运行时得到的结果:
错误1:
找不到Chrome Path
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
我怎么能绕过这个?
代码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/")
需要设置路径中的Chrome exe,无法读取捆绑的chrome.因此,封装的镀铬不能按预期工作.
解决方法:
使用–add-binary在应用程序中捆绑驱动程序:
pyinstaller -F --add-binary "C:\drivers\chromedriver.exe";"." script.py
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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。