0

I'm using RoR trying to search a simple form at my college using mechanize. The code works fine for searching google, but returns the search form in the results? I'm really confused. Any advice? Thanks!

ruby script/console
require 'mechanize'
agent = WWW::Mechanize.new
agent.get("https://www.owens.edu/cgi-bin/class.pl/")
agent.page.forms
form = agent.page.forms.last
form.occ_subject = "chm"
form.submit
blahdiblah
  • 33,069
  • 21
  • 98
  • 152
JZ.
  • 21,147
  • 32
  • 115
  • 192
  • Seems to work just fine for me. What problem are you having? Also, you're going to get a lot of test data if you leave a complete script that submits data on your SO post. But that might not be bad. – Chuck Vose Dec 08 '09 at 16:27

1 Answers1

0

I've solved it! When form.submit is being called, it is assuming the last button in form.buttons is the button to use. The last button in form.buttons is for the advanced form, hence the resulting page object being another form, albeit the more comprehensive advanced search form.

require 'mechanize'
agent = WWW::Mechanize.new
agent.get("https://www.owens.edu/cgi-bin/class.pl/")
agent.page.forms
form = agent.page.forms.last
form.occ_subject = "chm"
result = agent.submit(form, form.buttons.first)

result.parser.css('table.cs-table-settings tr.tbl-class-fill-b td font b').map { |v| v.text.strip }

=> ["Principles of Chemistry", "Principles of Chemistry", "Principles of Chemistry", "Principles of Chemistry", …]

Finally we get to the bottom of it! The HTML is horrible, so you will need to put your XPath hat on for this one! :)

Steve Graham
  • 3,001
  • 1
  • 22
  • 26
  • Awesome! I thought it was a button issue. I might now be able to program my boss out of a job - or at least save the poor guy some time for other things. – JZ. Dec 08 '09 at 23:09