我正在使用ruby中的WebDriver打开一组URL - 这种幻灯片在“幻灯片”(页面)之间间隔3秒。查看发生这种情况的人可能会单击Space,我需要将该页面URL保存到另一个文件中。我怎么能处理这些中断 - 抓住Space的事件?
require "watir-webdriver"
urls = [...list of URLs here...]
saved = []
b = Watir::Browser.new
urls.each do |url|
b.goto url
sleep(3)
# ...what should I put here to handle Space pressed?
if space_pressed
saved << b.url
end
end
答案 0 :(得分:3)
看起来您的问题可以通过STDIN.getch解决。
如果使用以下脚本创建文件,然后在命令提示符下运行它(例如“ruby script.rb”),脚本将:
Timeout::timeout(10)
行中将时间更改回3秒。脚本:
require "watir-webdriver"
require 'io/console'
require 'timeout'
urls = ['www.google.ca', 'www.yahoo.ca', 'www.gmail.com']
saved = []
b = Watir::Browser.new
urls.each do |url|
b.goto url
# Give user 10 seconds to provide input
puts "Capture url '#{url}'?"
$stdout.flush
input = Timeout::timeout(10) {
input = STDIN.getch
} rescue ''
# If the user's input is a space, save the url
if input == ' '
saved << b.url
end
end
p saved
几点说明: