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

问题描述 投票:0回答:4

我是 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,但在运行脚本时看到相同的错误。

ruby linux selenium heroku selenium-webdriver
4个回答
5
投票

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

默认情况下,Heroku 的测功机上未安装 Chrome。您需要使用安装 Chrome 的构建包。例如:

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

您可以看到它如何获取 Chrome:

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


1
投票

回答

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

1
投票

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

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

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

让我知道进展如何!


0
投票

我知道这来得有点晚了。但我也遇到了同样的问题,并且刚刚解决了。我快疯了。事实证明 Heroku 并没有真正维护他们的构建包。因此,安装这些构建包:

https://github.com/awl19/heroku-buildpack-google-chrome https://github.com/awl19/heroku-buildpack-chromedriver

当然还有这两个之后的 Heroku/Ruby 构建包。

繁荣。它会像发条一样工作。另外非常重要的是,不要忘记在每次请求后或在一定时间后显式退出浏览器,否则它将无限期地产生,将耗尽所有内存,并最终杀死服务器。你可以这样做

session.quit

我想对你来说(无论你在哪里调用新的水豚会话..也许在你的控制器中?)它应该看起来像这样:

session = Capybara::Session.new(:selenium)
   // your browser actions or logic here

 session.quit
© www.soinside.com 2019 - 2024. All rights reserved.