I am using Mechanize and Nokogiri in my Ruby on Rails application to scrape our local printers administration panel to retrieve the number of printed pages in the printers lifetime.
I have the following rake task:
# Logs into printer admin page and retrieved counts.
require 'rubygems'
require 'mechanize'
require 'logger'
# Create a new mechanize object
agent = Mechanize.new
# Load the printer admin page
page = agent.get("http://192.168.1.126/index.html?lang=1")
# Select the form with an action of index.cqi
form = agent.page.form_with(:action => "index.cgi")
form.radiobuttons_with(:id => '0x3fdb24153404')[1]
# Submit the form
page = form.submit form.buttons.first
pp page
That returns the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<script type="text/javascript">
<!--
window.onload=function(){setTimeout(function(){document.menu_link.submit();},0);}
//-->
</script>
</head>
<body>
<form name="menu_link" action="index.html" method="post" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="lang" value="1">
</form>
</body>
</html>
I don't seem to be able to select the form on the above page, and the script seems to stop at that page and not follow the redirect.
Is there a standard way of working with these kind of redirects? Maybe pausing the script until the redirect has taken place? Would it every allow the redirect to work?
Any pointers would be appreciated!