I wrote this bit of code to be called in a script that runs on a cron job, knowing it has to be a headless browser to be ran in a cron job, i found Headless. It sounds like a wonderful gem to do exactly what i want it do to, the only problem is it still opens up FireFox when I run the code.
I thought the whole point of headless was to not have to access the display and run in the background, like :phantomjs. Am I missing something or did I mistake what the headless gem is supposed to accomplish? (P.S. at bottom about when i tried to use :phantomjs)
#encoding: utf-8
require 'watir-webdriver'
require 'headless'
#log into admin dashboard
headless = Headless.new
browser = Watir::Browser.start 'http://app.mycompany.com/admin'
browser.link(:xpath =>'/html/body/div/div/div/div/a').when_present.click
browser.text_field(:id => 'Email').when_present.set 'me@mycompany.com'
browser.button(:id => 'next').click
browser.text_field(:id => 'Passwd').when_present.set 'password'
browser.button(:id => 'signIn').click
browser.goto 'https://app.mycompany.com/admin/dashboard'
#browser is at dashboard to grab yesterday's numbers
code that grabs data
#closes browser after grabbing data
browser.close
headless.destroy
#send timestamp
current_time = Time.now
puts "Screen grabbed at " + current_time.inspect + "\n\n"
#puts all data into array then outputs array split on each metric's title
dailyreportdata = [my glorious array of data]
dailyreportdata.each_slice(2) { |x|
puts x.join
}
My script runs to completion, but the data doesnt appear so I am guessing that it fails over when it tries to load the browser, thus loading no data to grab and send to my file.
My script look like this:
#!/bin/sh
_now=$(date +"%m_%d_%Y")
ruby dailyreportscraper.rb > ~/dailyscrape_$_now.txt
if I run it outside of the cron job, it works just fine.
P.S. - I tried phantomjs but every time it got to the "Enter Email" field, it would time out on waiting for the element to appear - it is a google login so maybe there is something to do with that, I even tried using the xpath as well.
Thanks for the help!