简体   繁体   English

以编程方式在Ruby中使用ScreenShot of Desktop?

[英]Programmatically take ScreenShot of Desktop in Ruby?

I asked this question about taking a picture of a webpage programmatically , and I've downloaded and got webkit2png working (taking pictures of HTML pages like blogs and whatnot). 我问过这个关于以编程方式拍摄网页图片的问题,我已经下载并使webkit2png正常工作(拍摄像博客和诸如此类的HTML页面的照片)。 So cool, thanks for showing me that! 太酷了,谢谢你告诉我的!

Now I would like to start doing more, like being able to take pictures of Flash websites after they have loaded, and of my desktop. 现在我想开始做更多事情,比如能够在加载Flash桌面和桌面后拍摄Flash网站。

Is it possible to take pictures of Flash websites with webkit2png (considering you might have to wait a few seconds for it to load)? 是否可以使用webkit2png拍摄Flash网站(考虑到你可能需要等待几秒才能加载)?

But the main question is, how do I programmatically take pictures of the desktop? 但主要的问题是,我如何以编程方式拍摄桌面照片? That would allow me to have much more control over what's going on. 这将使我能够更好地控制正在发生的事情。

您可以使用xwd(1)截取根窗口的屏幕截图:

xwd -display :0 -root|xwdtopnm |pnmtopng > $1

This is an awesome question! 这是一个很棒的问题!

A couple of years ago I has to work on a similar project. 几年前,我必须从事类似的项目。 I found a library, called watir , you can use to control system browsers from Ruby. 我发现了一个名为watir的库,可以用来控制Ruby的系统浏览器。 At the time I checked, it wasn't really reliable in a Linux environment, but right now it seems to be pretty solid. 在我检查的时候,它在Linux环境中并不真正可靠,但是现在它看起来非常可靠。

Here's a couple of links: 这里有几个链接:

I have never tried this solution so I would be really happy if you can write here a feedback if you decide to go with Watir. 我从未尝试过这个解决方案,所以如果你决定和Watir一起去,可以在这里写一个反馈,我会很高兴的。 All the examples targets a Windows server, I didn't found a valid tutorial using a Linux + Firefox environment. 所有示例都以Windows服务器为目标,我没有找到使用Linux + Firefox环境的有效教程。

You can use Watir WebDriver like this: 您可以像这样使用Watir WebDriver

$ irb
irb(main):001:0> require 'watir-webdriver'
=> true
irb(main):002:0> browser = Watir::Browser.new
=> #<Watir::Browser:0x136da92fad77d562 url="about:blank" title="">
irb(main):003:0> browser.goto 'http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby'
=> "http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby"
irb(main):004:0> browser.screenshot.save 'screenshot.png'
=> #<File:screenshot.png (closed)>
irb(main):005:0>

which seems to do the job rather well! 这似乎做得很好! I tested this on my Mac, but I'd be surprised if it didn't work well on Linux too. 我在Mac上对此进行了测试,但如果在Linux上也不能正常工作,我会感到惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby + Selenium-执行每个步骤后截屏 - Ruby + Selenium - Take a screenshot with each step executed Ruby Capybara Poltergeist,如何为具有所需登录名的页面拍摄屏幕截图 - Ruby Capybara Poltergeist, how to take screenshot for a page with required login 如何以编程方式获取已爬网页的快照(在Ruby中)? - How to Programmatically take Snapshot of Crawled Webpages (in Ruby)? 如何使用Ruby在Selenium Webdriver中使用屏幕快照名称中包含日期和时间的屏幕快照? - How to take screenshot in selenium webdriver with ruby with date and time included in screenshot name? 拍摄场景失败截图 - Take a scenario failure screenshot 谁能建议如何使用 ruby​​ selenium 截取整个网页的屏幕截图? - Can anyone suggest how to take screenshot of full webpage using ruby selenium? 如何在 Ruby 中使用 selenium-webdriver/capybara 截取完整浏览器页面及其元素的屏幕截图? - How to take a screenshot of a full browser page and its elements using selenium-webdriver/capybara in Ruby? 使用watir扩展元素截图截屏 - Take screenshot using watir extension element screenshot 使用 Ashot 和 Watir 截屏 - Take Screenshot using AShot and Watir 使用Ruby捕获Steam页面的屏幕截图 - Capture screenshot of Steam page with Ruby
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM