Python Selenium火狐脚本部署Heroku时geckodriver退出码64如何解决
解决方案
问题原因
你遇到的报错是geckodriver异常退出,状态码64通常由两个核心问题导致:
- Selenium 4+版本不再支持旧写法的
executable_path、firefox_binary参数,参数传递错误导致驱动启动失败 - Heroku环境共享内存不足、驱动与浏览器版本不匹配、启动参数配置不当
修复步骤
1. 调整Selenium初始化代码,适配Selenium 4+规范
替换原有driver初始化相关代码,改用Service对象配置驱动路径,修正启动参数:
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.firefox.service import Service from bs4 import BeautifulSoup import re import yagmail import time import os options = webdriver.FirefoxOptions() options.log.level = "trace" # 修正参数写法,新增适配Heroku环境的参数 options.add_argument("--headless=new") options.add_argument("--disable-gpu") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") # 直接通过options指定浏览器路径,不需要FirefoxBinary options.binary_location = os.environ.get('FIREFOX_BIN') # 用Service配置geckodriver路径 service = Service(executable_path=os.environ.get('GECKODRIVER_PATH')) rea_inicial='teste' while True: try: driver = webdriver.Firefox(service=service, options=options) driver.get("https://biblioteca.aneel.gov.br/Busca/Avancada") print('entrando no site...') # Busca por legislacoo driver.find_element(By.XPATH, '/html/body/main/div/div/div[2]/div/div/button[2]').click() wait = WebDriverWait(driver, 30) print('chegou ate aqui') # 你的业务逻辑写在这里 finally: # 每次循环结束关闭驱动,避免资源泄漏 driver.quit() time.sleep(10) # 按需调整循环间隔
2. 固定依赖版本
在你的requirements.txt文件中指定兼容的Selenium版本,避免自动更新到不兼容的最新版:
selenium==4.8.3
3. 验证环境变量配置
确认你配置的Config Vars没有拼写错误,路径与buildpack生成的路径一致,你当前配置的路径是正确的,不需要修改。
内容的提问来源于stack exchange,提问作者Luís Eduardo
相关产品推荐
相关产品推荐

