我在heroku日志中收到一个错误:"'chromedriver.exe的可执行文件需要在PATH中。请参阅https://sites.google.com/a/chromium.org/chromedriver/home">
我试着在命令行中运行这一行:
heroku config:set CHROMEDRIVER_PATH=/app/.heroku/python/lib/python3.9/site-packages/CHROMEDRIVER和我得到了一个不同的错误:PermissionError:[Erno 13]权限被拒绝:"/app/.horoku/pythn/lib/ppython3.9/site-packs/CHROMEDRIVER'
以及其他几种给我原始错误的配置,包括:
/彩色打印机彩色打印机/nfs/stak/users/croysdaa/361 chromedriver
以及将我的python文件app.py更改为:
GOOGLE_CHROME_PATH = '/app/.apt/usr/bin/google_chrome'
CHROMEDRIVER_PATH = '/app/.heroku/python/lib/python3.9/site-packages/chromedriver'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.binary_location = GOOGLE_CHROME_PATH
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, chrome_options=chrome_options)
chromedriver在我的requirements.txt文件中,它在我当前的目录中
我在heroku配置中编辑了googlechrome路径和chromedriver路径(可以在终端或heroku.com上完成(,如下两条评论中提到的。由于我从Heroku而不是本地获取路径变量,所以我使用os.environ.get(和导入os(来获取路径
#GOOGLE_CHROME_PATH = '/app/.apt/usr/bin/google-chrome'
#CHROMEDRIVER_PATH = '/app/.chromedriver/bin/chromedriver'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
#chrome_options.add_argument('--proxy-sever=socks5://127.0.0.1:0000')
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("window-size=1200x600")
chrome_options.add_argument("--window-position=0,0")
chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
# exception for if chromedriver crashes on launch
try:
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options)
except WebDriverException as e:
print("nChrome crashed on launch:")
print(e)
print("Trying again in 1 second")
time.sleep(1)
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options)
print("Success!n")
except Exception as e:
raise Exception(e)