ruby-on-rails - 我怎样才能在 heroku 上使用 firefox

标签 ruby-on-rails firefox heroku watir buildpack

我正在使用 watir webdriver 及其 headless 功能以及 firefox 浏览器转到网站,比如 www.xyz.com。单击不同的按钮并下载 pdf。我已经在我的本地环境。当我将我的应用程序推送到 heroku 时,它要求构建包。我添加了构建包,它们存在于我的 heroku 中。我通过运行 heroku run bash 找到了它。 使用的依赖项是:

gem 'watir-webdriver', '~> 0.9.1'
gem 'headless', '~> 2.2', '>= 2.2.3'
Buildpack of Xvfb
Buildpack of firefox 

Xvfb buildpack 工作正常。当运行 browser = Watir::Browser.new(:firefox, :profile => profile) 时,我收到奇怪的错误,例如 Selenium::WebDriver::Error::WebDriverError:无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1:7055)为 firefox 设置路径。我已经为 firefox Selenium::WebDriver::Firefox::Binary.path='vendor/firefox/firefox-bin' 设置了路径 .我可以在我的 heroku bash 中看到安装在该位置的 firefox。 我不确定这是 heroku 问题还是 buildpack 问题。尽管 buildpack 的开发者说他无法在 heroku 上运行他的 firefox buildpack 但他能够在 Amazon EC2 上部署。是否可以在 heroku 上安装和使用 firefox 及其所有功能(如打开和关闭浏览器、下载 pdf、打开选项卡)?如果我的问题不太清楚,我很抱歉。 Firefox Buildpack .

begin
  ActiveRecord::Base.transaction do
    download_directory = "#{Rails.root}/tmp/#{dir_name}"

    Selenium::WebDriver::Firefox::Binary.path='vendor/firefox/firefox-bin'
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile['download.prompt_for_download'] = false
    profile['browser.download.folderList'] = 2 # custom location
    profile['browser.download.dir'] = download_directory
    profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"
    # Disable built-in pdf viewer of Firefox browser
    profile['pdfjs.disabled'] = true
    profile['pdfjs.firstRun'] = false

    headless = Headless.new
    headless.start

    browser = Watir::Browser.new(:firefox, :profile => profile)
    # browser.screenshot.save "pp.png"
    browser.goto 'xyz.com'
    browser.window.resize_to(some_x,some_y)
    browser.text_field(:name => "some_name").set("#{some_data}")
    browser.text_field(:name => "some_password").set("#{password}")
    browser.button(:name => "button").click
    #Pdf gets downloaded in the defined location
    #some database updations
    headless.destroy
  end
rescue => r
end

最佳答案

您不能在 heroku 上将 Firefox 用作 headless 浏览器。 Heroku 不支持此功能。您可以使用一些 JavaScript 浏览器,例如 Phantom js .

您可以在 heroku 上为 pantom js 配置 buildpack。 您需要进行一些代码更改,例如:

Selenium::WebDriver::PhantomJS.path = 'path_to_phantomjs'
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 180 # seconds – default is 60
args = %w{--ignore-ssl-errors=true}
browser = Watir::Browser.new :phantomjs, :http_client => client, :args => args

关于ruby-on-rails - 我怎样才能在 heroku 上使用 firefox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39167995/

相关文章:

ruby-on-rails - ZenTest 4.9.3 显示为无效的 gemspec

css - 相当于 FF 和 IE 的 -webkit-text-stroke

python - 在Heroku上的flask应用程序中无法识别Google Analytics(分析)跟踪

javascript - 找不到模块 : error when deployed on Heroku

postgresql - 物理恢复 heroku postgresql 数据库

ruby-on-rails - 直接从 rails Class.find( :all) array of objects

ruby-on-rails - Rails 什么时候响应 'transfer-encoding' 与 'content-length' ?

javascript - ruby rails : How to Render Partial in a view via Jquery

html - Firefox 和 Chrome 中计算高度的差异

authentication - 如何在没有 key 卡的情况下使用 webauthn