首页 > 解决方案 > 无法使用 python 脚本在 heroku 上运行 selenium

问题描述

我正在尝试运行一个简单的 python 脚本来打开 firefox 并在https://biblioteca.aneel.gov.br/上搜索一些信息。

在我的电脑上脚本可以运行,但是当我尝试在 heroku 上运行时,它总是崩溃。我尝试了所有我能找到的教程,但我不知道还能做什么。

我做了:

  1. 我在 heroku 上放置了“Buildpacks”:https ://github.com/pyronlaboratory/heroku-integrated-firefox-geckodriver
  2. 在 heroku 上的“配置变量”中:
    • FIREFOX_BIN = /app/vendor/firefox/firefox
    • GECKODRIVER_PATH = /app/vendor/geckodriver/geckodriver
    • LD_LIBRARY_PATH = /usr/local/lib:/usr/lib:/lib:/app/vendor
    • PATH = /usr/local/bin:/usr/bin:/bin:/app/vendor/
  3. 在heroku上调整代码:
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.firefox_binary import FirefoxBinary
from bs4 import BeautifulSoup
import re
import yagmail
import time
import os

options = webdriver.FirefoxOptions()
# enable trace level for debugging 
options.log.level = "trace"
options.add_argument("-remote-debugging-port=9224")
options.add_argument("-headless")
options.add_argument("-disable-gpu")
options.add_argument("-no-sandbox")
binary = FirefoxBinary(os.environ.get('FIREFOX_BIN'))

rea_inicial='teste'
while True:
    driver = webdriver.Firefox(firefox_binary=binary, executable_path=os.environ.get('GECKODRIVER_PATH'), options=options)
    #driver.maximize_window()
    driver.get("https://biblioteca.aneel.gov.br/Busca/Avancada")
    print('entrando no site...')
    #time.sleep(5)

    # 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')

然后,在heroku上部署后的错误:

2021-11-05T17:40:55.035807+00:00 app[worker.1]: code.py:24: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
2021-11-05T17:40:55.035820+00:00 app[worker.1]: driver = webdriver.Firefox(firefox_binary=binary, executable_path=os.environ.get('GECKODRIVER_PATH'), options=options)
2021-11-05T17:40:55.035825+00:00 app[worker.1]: code.py:24: DeprecationWarning: firefox_binary has been deprecated, please pass in a Service object
2021-11-05T17:40:55.035825+00:00 app[worker.1]: driver = webdriver.Firefox(firefox_binary=binary, executable_path=os.environ.get('GECKODRIVER_PATH'), options=options)
2021-11-05T17:40:55.540760+00:00 app[worker.1]: Traceback (most recent call last):
2021-11-05T17:40:55.540774+00:00 app[worker.1]: File "code.py", line 24, in <module>
2021-11-05T17:40:55.540947+00:00 app[worker.1]: driver = webdriver.Firefox(firefox_binary=binary, executable_path=os.environ.get('GECKODRIVER_PATH'), options=options)
2021-11-05T17:40:55.540949+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 175, in __init__
2021-11-05T17:40:55.541113+00:00 app[worker.1]: self.service.start()
2021-11-05T17:40:55.541123+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 101, in start
2021-11-05T17:40:55.541249+00:00 app[worker.1]: self.assert_process_still_running()
2021-11-05T17:40:55.541250+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 113, in assert_process_still_running
2021-11-05T17:40:55.541359+00:00 app[worker.1]: raise WebDriverException(
2021-11-05T17:40:55.541409+00:00 app[worker.1]: selenium.common.exceptions.WebDriverException: Message: Service /app/vendor/geckodriver/geckodriver unexpectedly exited. Status code was: 64

谁能帮我解决它?

标签: pythonseleniumselenium-webdriverherokufirefox

解决方案


以下论据:

  • -no-sandbox
  • -disable-gpu

也许适用于ChromeDriver / 组合,但不适用于GeckoDriver / 组合。

删除参数并重新执行。


推荐阅读