2016-07-08 77 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的。

讓我知道它是怎麼回事!