It looks like your problem might be solvable with STDIN.getch.
If you create a file with the following script and then run it in a command prompt (eg "ruby script.rb"), the script will:
- Navigate to the url.
- Ask if the url should be captured.
- If the user does not input anything in 10 seconds, it will proceed onto the next url. I changed it from 3 since it was too fast. You can change the time back to 3 seconds in the line
Timeout::timeout(10)
.
- If the user did input something, it will save the url if the input was a space. Otherwise it will ignore it and move on.
Script:
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
A couple of notes:
- Note that the inputs need to be to the command prompt rather than the browser.
- If the user presses a key before the allotted timeout, the script will immediately proceed to the next url.