简体   繁体   English

在 Heroku 上使用 Python selenium 运行 ChromeDriver

[英]Running ChromeDriver with Python selenium on Heroku

So I have a Flask server on Heroku which has been working fine as expected for some time.Now, as per new requirements, I need to add functionality to the Flask server to fetch a page from an external website.Because of reasons best known to me, I am using Selenium along with Chrome web driver to do this.Locally I was able to set this up and it works fine but I am quite unsure as to how to set it up on the Heroku server.所以我在 Heroku 上有一个 Flask 服务器,它已经按预期正常工作了一段时间。现在,根据新的要求,我需要向 Flask 服务器添加功能以从外部网站获取页面。由于众所周知的原因我,我正在使用 Selenium 和 Chrome 网络驱动程序来做到这一点。在本地我能够设置它并且它工作正常,但我不确定如何在 Heroku 服务器上设置它。 I read a bit about buildpacks and found this buildpack for ChromeDriver :我阅读了一些关于 buildpacks 的内容,并为 ChromeDriver 找到了这个 buildpack:

https://elements.heroku.com/buildpacks/jimmynguyc/heroku-buildpack-chromedriver

However, I am not sure how to proceed further.How do I install chromium browser itself and what else is needed to tie it all up ?但是,我不确定如何进一步进行。如何安装 Chrome 浏览器本身以及还需要什么来将其全部绑定?

I had the same issue and the following steps worked fine for me:我遇到了同样的问题,以下步骤对我来说效果很好:

  • I added the following buildpacks on heroku: https://github.com/heroku/heroku-buildpack-xvfb-google-chrome (to install chrome, since chromedriver requires it) and https://github.com/heroku/heroku-buildpack-chromedriver .我在 heroku 上添加了以下 buildpack: https : //github.com/heroku/heroku-buildpack-xvfb-google-chrome (安装 chrome,因为 chromedriver 需要它)和https://github.com/heroku/heroku- buildpack-chromedriver
  • I created an environment variable GOOGLE_CHROME_BIN, with the path of chrome on heroku: /app/.apt/usr/bin/google-chrome and an environment variable called CHROMEDRIVER_PATH with the path of chromedriver on heroku: /app/.chromedriver/bin/chromedriver.我创建了一个环境变量 GOOGLE_CHROME_BIN,在 heroku 上使用 chrome 的路径:/app/.apt/usr/bin/google-chrome 和一个名为 CHROMEDRIVER_PATH 的环境变量,在 heroku 上使用 chromedriver 的路径:/app/.chromedriver/bin/铬驱动程序。
  • In my python file, I configured chromedriver:在我的 python 文件中,我配置了 chromedriver:

     chrome_options = Options() chrome_options.binary_location = GOOGLE_CHROME_BIN chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('--no-sandbox') driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, chrome_options=chrome_options)

(First, I tried to configure chromedriver with no arguments, but I faced the following error: "Chrome failed to start: crashed". --disable-gpu and --no-sandbox solved the problem for me). (首先,我尝试在没有参数的情况下配置 chromedriver,但我遇到了以下错误:“Chrome 无法启动:崩溃”。--disable-gpu 和 --no-sandbox 为我解决了这个问题)。

In your Heroku app go to Settings and add the following build packs:在您的 Heroku 应用程序中,转到“设置”并添加以下构建包:

In addition, in your Python script you have to set a few Chrome options so that your script runs on Heroku without error.此外,在您的 Python 脚本中,您必须设置一些 Chrome 选项,以便您的脚本在 Heroku 上运行而不会出错。

import time

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

gChromeOptions = webdriver.ChromeOptions()
gChromeOptions.add_argument("window-size=1920x1480")
gChromeOptions.add_argument("disable-dev-shm-usage")
gDriver = webdriver.Chrome(
    chrome_options=gChromeOptions, executable_path=ChromeDriverManager().install()
)
gDriver.get("https://www.python.org/")
time.sleep(3)
gDriver.save_screenshot("my_screenshot.png")
gDriver.close()

Here is a more detailed post that I created in case you are still having problems: https://www.jtrocinski.com/posts/Heroku-Use_Selenium_to_run_Google_Chrome_in_Python.html这是我创建的更详细的帖子,以防您仍然遇到问题: https : //www.jtrocinski.com/posts/Heroku-Use_Selenium_to_run_Google_Chrome_in_Python.html

For pyppeteer对于 pyppeteer

If anybody is running on Heroku and facing the same error:如果有人在Heroku上运行并面临同样的错误:

Add the buildpack : The url for the buildpack is below :添加buildpackbuildpack的 url 如下:

https://github.com/jontewks/puppeteer-heroku-buildpack

Ensure that you're using --no-sandbox mode确保您使用的是--no-sandbox模式

launch({ args: ['--no-sandbox'] })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM