python - 无法使用 python 脚本在 heroku 上运行 selenium
问题描述
我正在尝试运行一个简单的 python 脚本来打开 firefox 并在https://biblioteca.aneel.gov.br/上搜索一些信息。
在我的电脑上脚本可以运行,但是当我尝试在 heroku 上运行时,它总是崩溃。我尝试了所有我能找到的教程,但我不知道还能做什么。
我做了:
- 我在 heroku 上放置了“Buildpacks”:https ://github.com/pyronlaboratory/heroku-integrated-firefox-geckodriver
- 在 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/
- 在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
谁能帮我解决它?
解决方案
以下论据:
-no-sandbox
-disable-gpu
也许适用于ChromeDriver / google-chrome组合,但不适用于GeckoDriver / firefox组合。
删除参数并重新执行。
推荐阅读
- r - 使用不包含字符串的正则表达式过滤闪亮的 DT
- uima - 如何声明#除了换行符以供以后使用?
- javascript - 如何在 Atom 中禁用正则表达式
- json - 如何使用 iOS Swift 访问设备中的 vpn api url?
- javascript - 在构造函数中将函数绑定到“this”有什么作用?
- angular - 没有错误,但 MessageService 不会显示任何内容
- excel - Excel - 相当于Javascript减少功能?
- mysql - 在我的数据库中添加具有“可为空”属性和默认值的新列时,如何在我的代码中保持相同的“INSERT INTO”语句?
- r - 如何从 R 到 Hive 表
- c++ - 使用“现代 C++ 的 JSON”库检测整数不适合指定类型?