I’m trying to create an automated script that runs through cron, but I’m having issues with the Headless gem. Even though I’m using it, Firefox still opens a visible window when the script executes.
require 'watir-webdriver'
require 'headless'
# start headless session
virtual_display = Headless.new
virtual_display.start
# navigate to login page
driver = Watir::Browser.new :firefox
driver.goto 'https://portal.example.com/login'
# perform login steps
driver.link(:css => '.auth-link').wait_until_present
driver.link(:css => '.auth-link').click
driver.text_field(:name => 'username').set '[email protected]'
driver.button(:class => 'continue-btn').click
driver.text_field(:name => 'password').set 'mypassword'
driver.button(:class => 'login-btn').click
# navigate to main page
driver.goto 'https://portal.example.com/reports'
# extract data here
data_collection = []
# data extraction logic
# cleanup
driver.quit
virtual_display.destroy
print "Data extracted at: #{Time.now}\n"
data_collection.each { |item| puts item }
My shell script is:
#!/bin/bash
today=$(date +"%Y%m%d")
ruby data_scraper.rb > ~/reports/extract_$today.log
The script works fine when I run it manually, but fails in cron. I think the visible Firefox window is the problem. Shouldn’t Headless prevent any GUI from appearing? I tried PhantomJS before but it couldn’t handle the Google authentication properly.