How to take screenshot of a website with Rails 3.1? — without using a service

后端 未结 4 403
[愿得一人]
[愿得一人] 2021-01-31 12:02

Almost every answer I\'ve found references using some existing service. Is there a way to do this using Rails 3.1 programmatically? This was dead easy to do wit

4条回答
  •  孤城傲影
    2021-01-31 12:29

    There is a Rails gem for this task.

     gem install selenium-webdriver
    

    Simple use case:

    require 'selenium-webdriver'
     width = 1024
     height = 728
     driver = Selenium::WebDriver.for :firefox
     driver.navigate.to 'http://domain.com'
     driver.execute_script %Q{
       window.resizeTo(#{width}, #{height});
     }
     driver.save_screenshot('/tmp/screenshot.png')
     driver.quit
    

提交回复
热议问题