用Heroku上的Python selenium运行ChromeDriver [英] Running ChromeDriver with Python selenium on Heroku

查看:198
本文介绍了用Heroku上的Python selenium运行ChromeDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在Heroku上有一台Flask服务器,在一段时间内一直工作正常。现在,根据新的要求,我需要向Flask服务器添加功能以从外部网站获取页面。由于原因我最熟悉的是,我使用Selenium和Chrome网络驱动程序来做到这一点。我可以设置它,它工作正常,但我很不确定如何在Heroku服务器上进行设置。我读了一些关于buildpack的内容,并发现ChromeDriver的buildpack:

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

然而,我不确定如何继续进一步操作。如何安装铬浏览器本身,还有什么需要把它捆绑?

解决方案

我有同样的问题,以下步骤工作正常对我来说:


  • 我在heroku上添加了以下构建包:

    (要安装chrome,因为chromedriver需要它)和
    https://github.com/heroku/heroku-buildpack -chromedriver

  • 我创建了一个环境变量GOOGLE_CHROME_BIN,其中h的路径为chrome eroku:/app/.apt/usr/bin/google-chrome和一个名为CHROMEDRIVER_PATH的环境变量,其路径为heroku:/app/.chromedriver/bin/chromedriver。

  • 在我的python文件中,我配置了chromedriver:

      chrome_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)




(首先,我试着配置chromedriver没有参数,但我遇到了以下错误:Chrome无法启动:崩溃。 --disable-gpu和--no-sandbox解决了我的问题)。


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. I read a bit about buildpacks and found this buildpack for ChromeDriver :

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 ?

解决方案

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.
  • 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.
  • In my python file, I configured 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).

这篇关于用Heroku上的Python selenium运行ChromeDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆