2016-07-08 73 views
5

因为涉及到linux安装(和heroku),所以我是个noob,所以如果这个问题很基本,请大家道歉。用heroku上的chrome驱动运行selenium:`找不到chrome二进制`

我想在Heroku上运行selenium webkit(在ruby中)。我面临一个难题,即我的脚本无法找到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。该宝石应该使二进制文件可用于红宝石进程,但没有。

我也尝试明确设置路径,例如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>' 

UPDATE:

我试图与AWS EC2服务器相同(例如推出,克隆混帐回购协议,安装了所有的依赖)。同样的情况也发生在那里。也就是说,能够从终端执行chromedriver,但在运行脚本时看到相同的错误。

+0

你有没有考虑过运行[Poltergeist](https://github.com/teampoltergeist/poltergeist)呢? –

+0

@IlyaVassilevsky不,我还没有试过Poltergeist。我不知道这个错误是否与有“头”的chromedriver有关联 –

+0

链接的SO问题听起来对我来说就像它正是你遇到的问题 –

回答

6

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

默认情况下,Heroku没有在其dynos上安装Chrome。您需要使用安装Chrome的buildpack。例如:

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

你可以看到它是如何获取浏览器:

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

+0

谢谢@Ilya。我试过这个buildpack,但没有奏效。我怀疑在heroku上安装谷歌浏览器(或任何浏览器)可能会涉及更多。 –

+1

感谢您的回答伊利亚。因为我无法让Heroku安装Chrome,所以我最终与Watir一起用phantomjs重新编写了代码。 –

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 

背景

下面的例子显示了我在最近的Ruby项目设置为硒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 
+1

嘿,男士,谢谢你的回答。我尝试过使用它,包括在代码以及irb控制台中完成以上所有内容。我也尝试在bundle文件夹('/ app/vendor/bundle/bin/chromedriver')中对可执行文件进行硬编码。我尝试了'CHROMEDRIVER_FN'的打印值。 'CHROMEDRIVER_FN'设置正确。该文件绝对存在,但Selenium在运行时没有看到它。 –

+0

我为此开始了恩惠 –

+0

太糟糕了。至少我们现在知道Heroku是这里的因素......祝你好运! – SoAwesomeMan

1

下面是对我工作的最小配置。你需要有合适的buildpack才能安装chrome,看起来你只需要安装chromedriver,这是一个单独的二进制文件。

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

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

让我知道它是怎么回事!