在heroku上使用chrome驱动程序运行selenium:`找不到Chrome二进制文件

时间:2016-07-08 04:16:20

标签: ruby linux selenium heroku selenium-webdriver

我是一个菜鸟,因为它涉及到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。宝石原本应该使二进制文件可用于ruby进程,但没有。

我还尝试明确设置路径,例如我的ruby代码中的Selenium::WebDriver::Chrome.driver_path = 'chromedriver',前面提到的文件位于根类别中。

这一切都在本地完美运作(有或没有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 repo,安装所有依赖项)。同样的事也发生在那里。也就是说,能够从终端执行chromedriver,但在运行脚本时看到相同的错误。

3 个答案:

答案 0 :(得分:5)

ChromeDriver is just a driver for Chrome. It needs the actual Chrome browser installed on the same machine to actually work.

Heroku doesn't have Chrome installed on its dynos by default. You need to use a buildpack that installs Chrome. For example:

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

You can see how it fetches Chrome:

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

答案 1 :(得分:1)

<强> ANSWER

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

<强> CONTEXT

以下示例显示了我在最近的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

答案 2 :(得分:1)

这是一个对我有用的最小配置。你也需要有正确的buildpack来安装chrome,看起来你只是安装了一个单独的二进制文件chromedriver。

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

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

让我知道它是怎么回事!