【问题标题】:How can I use firefox on heroku如何在heroku上使用firefox
【发布时间】:2016-08-26 13:52:37
【问题描述】:

我正在使用 watir webdriver 及其无头功能以及 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: unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055) 的奇怪错误 或set path for firefox。我已经为 Firefox Selenium::WebDriver::Firefox::Binary.path='vendor/firefox/firefox-bin' 设置了路径 .我可以在我的heroku bash 中看到安装在那个位置的firefox。 我不确定这是heroku问题还是buildpack问题。虽然buildpack的开发人员说他无法在heroku上运行他的firefox buildpack,但他能够在Amazon EC2上部署。可以安装和使用在heroku上具有所有功能(如打开和关闭浏览器、下载pdf、打开标签)的firefox?如果我对我的问题不太清楚,我很抱歉。 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

【问题讨论】:

    标签: ruby-on-rails firefox heroku watir buildpack


    【解决方案1】:

    您不能在 heroku 上使用 Firefox 作为无头浏览器。 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
    

    【讨论】:

    • 非常感谢 @Dipak G 先生。我试过你的脚本。我以前也试过 phantomjs 但失败了,因为我无法弄清楚file download using phantomjs。我做了很多谷歌搜索和一些旧帖子告诉我使用phantomjs无法下载文件。我不知道如何解决这个问题,它也适用于Heroku。我必须从网站下载pdf文件。我没有它的链接是 302(javascript 重定向)。我不确定这是否可以使用 mechanize gem 来实现。请建议我解决这个问题的可能方法。
    猜你喜欢
    • 1970-01-01
    • 2015-09-09
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2017-08-09
    • 1970-01-01
    相关资源
    最近更新 更多