Search code examples
rubyscreen-scrapingnokogirimechanizenomethoderror

How to insert a string to a text field using mechanize in ruby?


I know is a very simple question but I've been stuck for an hour and I just can't understand how this works.

I need to scrape some stuff from my school's library so I need to insert 'CE' to a text field and then click on a link with text 'Clasificación'. The output is what I am going to use to work. So here is my code.

require 'rubygems'
require 'open-uri'
require 'nokogiri'
require 'mechanize'

url = 'http://biblio02.eld.edu.mx/janium-bin/busqueda_rapida.pl?Id=20110720161008#'
searchStr = 'CE'

agent = Mechanize.new
page = agent.get(url)

searchForm = page.form_with(:method => 'post')
searchForm['buscar'] = searchStr

clasificacionLink = page.link_with(:href => "javascript:onClick=set_index_and_submit(\'51\');").click
page = agent.submit(searchForm,clasificacionLink)

When I run it, it gives me this error

janium.rb:31: undefined method `[]=' for nil:NilClass (NoMethodError)

Thanks!


Solution

  • mu's answer sounds reasonable. I am not sure if this is strictly necessary, but you might also try to put braces around searchStr.

    searchForm['buscar'] = [searchStr]