【问题标题】:run selenium with chrome driver on heroku: `cannot find Chrome binary`在 heroku 上使用 chrome 驱动程序运行 selenium:`找不到 Chrome 二进制文件`
【发布时间】:2023-04-01 22:06:01
【问题描述】:

我是 linux 设置(和 heroku)的菜鸟,如果这个问题是基本的,请道歉。

我想在 Heroku 上运行 selenium webkit(在 ruby​​ 中)。我遇到了一个问题,我的脚本找不到 Chrome 二进制文件。

我实际上让 chrome 自己工作:

~ $ chromedriver
Starting ChromeDriver 2.22.397932 (282ed7cf89cf0053b6542e0d0f039d4123bbb6ad) on port 9515
Only local connections are allowed.

chromedriver 是我从/app/vendor/bundle/bin/chromedriver 复制的一个文件,只是为了暂时方便。 chromedriver 文件存在,因为我安装了chromedriver-helper gem。 gem 应该使二进制文件可用于 ruby​​ 进程,但没有。

我也尝试过明确设置路径,例如Selenium::WebDriver::Chrome.driver_path = 'chromedriver' 在我的 ruby​​ 代码中,上述文件位于根目录中。

这一切都在本地完美运行(有或没有driver_path

可能是什么原因?几年前我读过this SO thread,但对我来说似乎已经过时了。任何想法将不胜感激!

错误跟踪:

~ $ ruby bin/run.rb
/app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/response.rb:70:in `assert_ok': unknown error: cannot find Chrome binary (Selenium::WebDriver::Error::UnknownError)
  (Driver info: chromedriver=2.22.397932 (282ed7cf89cf0053b6542e0d0f039d4123bbb6ad),platform=Linux 3.13.0-91-generic x86_64)
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/response.rb:34:in `initialize'
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/http/common.rb:78:in `new'
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/http/common.rb:78:in `create_response'
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/http/default.rb:90:in `request'
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/bridge.rb:649:in `raw_execute'
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/bridge.rb:123:in `create_session'
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/bridge.rb:87:in `initialize'
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/chrome/bridge.rb:48:in `initialize'
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/common/driver.rb:64:in `new'
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/common/driver.rb:64:in `for'
    from /app/vendor/bundle/ruby/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver.rb:84:in `for'
    from /app/lib/mealpass_orderer.rb:12:in `initialize'
    from /app/lib/mealpass_orderer.rb:8:in `new'
    from /app/lib/mealpass_orderer.rb:8:in `run'
    from bin/run.rb:3:in `<main>'

更新:

我对 AWS EC2 服务器进行了同样的尝试(启动实例、克隆 git 存储库、安装所有依赖项)。那里也发生同样的情况。也就是说,能够从终端执行 chromedriver,但在运行脚本时看到同样的错误。

【问题讨论】:

  • 您是否考虑过运行Poltergeist
  • @IlyaVassilevsky 不,我还没有尝试过 Poltergeist。我想知道这个错误是否与 chromedriver 有“头”有关
  • 链接的 SO 问题对我来说听起来正是您遇到的问题

标签: ruby linux selenium heroku selenium-webdriver


【解决方案1】:

这是对我有用的最小配置。您还需要有正确的构建包来安装 chrome,看起来您只是在安装 chromedriver,它是一个单独的二进制文件。

https://github.com/jormon/minimal-chrome-on-heroku-xvfb

您可以使用 README.md 上的按钮测试一键式将其部署到 Heroku。

告诉我进展如何!

【讨论】:

    【解决方案2】:

    ChromeDriver 只是 Chrome 的驱动程序。它需要在同一台机器上安装实际的 Chrome 浏览器才能真正工作。

    Heroku 默认没有在其测功机上安装 Chrome。您需要使用安装 Chrome 的 buildpack。例如:

    https://github.com/dwayhs/heroku-buildpack-chrome

    您可以查看它是如何获取 Chrome 的:

    https://github.com/dwayhs/heroku-buildpack-chrome/blob/master/bin/compile#L36-38

    【讨论】:

    • 谢谢@Ilya。我尝试了这个 buildpack,但它没有用。我怀疑在 heroku 上安装 google chrome(或任何浏览器)可能会涉及更多。
    • 是的,Heroku 是一个非常固执且封闭的平台。在您自己的 AWS、Linode 或 DigitalOcean 上的 VM 上使用 ChromeDriver 设置 Chrome 应该会容易得多。
    • 感谢您的回答伊利亚。我最终用 phantomjs 重写了 Watir,因为我无法让 Heroku 安装 Chrome。
    【解决方案3】:

    回答

    YOUR_PATH = 'whatever/your/path/is' # to your bin dir
    CURRENT_DIR = File.expand_path(File.dirname(__FILE__))
    CHROMEDRIVER_FN = File.join(CURRENT_DIR, YOUR_PATH, "bin/chromedriver")
    # —OR—
    #CHROMEDRIVER_FN = File.join(File.absolute_path('..', CURRENT_DIR), YOUR_PATH, "bin/chromedriver")
    Selenium::WebDriver::Chrome.driver_path = CHROMEDRIVER_FN
    

    上下文

    下面的示例显示了我在最近的一个 Ruby 项目中对 Selenium Chromedriver 的设置。

    1) 文件结构:

    ruby_app/
    ├── Gemfile
    ├── Gemfile.lock
    ├── History.txt
    ├── Manifest.txt
    ├── README.md
    ├── Rakefile
    ├── bin
    │   └── chromedriver
    ├── doc
    ├── lib
    │   └── ruby_app.rb
    └── test
        ├── test_files
        │   ├── test_config.yml
        │   └── uris_array_dump.yml
        ├── test_ruby_app.rb
        ├── test_google.rb
        ├── test_helper.rb
        └── test_output
    

    2) 在test/test_helper.rb:

    TEST_DIR = File.expand_path(File.dirname(__FILE__))
    TEST_FILES = File.join(TEST_DIR, "test_files")
    TEST_OUTPUT = File.join(TEST_DIR, "test_output")
    CHROMEDRIVER_FN = File.join(File.absolute_path('..', TEST_DIR), "bin", "chromedriver")
    

    以上代码使用File.absolute_path,见:http://ruby-doc.org/core-2.3.1/File.html#method-c-absolute_path

    将路径名转换为绝对路径名。相对路径是 从进程的当前工作目录引用,除非 dir_string 被给出,在这种情况下它将被用作起始 点。


    3) 在test/test_google.rb:

    Selenium::WebDriver::Chrome.driver_path = CHROMEDRIVER_FN
    

    【讨论】:

    • 你好,谢谢你的回答。我尝试使用它,包括在代码中以及在 irb 控制台中执行上述所有操作。我还尝试对捆绑文件夹 (/app/vendor/bundle/bin/chromedriver) 中的可执行文件进行硬编码。我尝试打印CHROMEDRIVER_FN 的值。 CHROMEDRIVER_FN 设置正确。该文件肯定存在,但 Selenium 在运行时看不到它。
    • 我为此开始了赏金
    • 太糟糕了。至少我们现在知道 Heroku 是这里的因素……祝你好运!
    • 谢谢!你知道如果没有在heroku上运行进程可能是原因吗?我没有运行服务器或其他进程...
    • “第三方构建包,使您能够使用 Heroku 官方支持的语言和框架之外的语言和框架。” github.com/tstachl/heroku-buildpack-selenium + devcenter.heroku.com/articles/third-party-buildpacks
    猜你喜欢
    • 2016-09-19
    • 2015-11-01
    • 2016-12-16
    • 1970-01-01
    • 2012-07-15
    • 2018-12-16
    • 2018-11-22
    • 1970-01-01
    • 2018-04-06
    相关资源
    最近更新 更多