通过<a> element with Ruby Mechanize

时间:2015-05-08 03:36:43

标签: html ruby mechanize mechanize-ruby

I've got a set of pages that I'm trying to scrape with Mechanize in Ruby. On some of the pages, they redirect to a page that wants me to fill out a select-list form and then submit it. The problem is with the button that submits the form, which is an <a> element. It looks like this:

<a class="css_class" href="#" onclick="RunSomeScript; return false;">
  <span>
    Enter
  </span>
</a>

The magic seems to be happening with the RunSomeScript script; it is the thing that seems to bypass the redirect page and take me to the page with the data that I'm trying to scrape. Unfortunately, I can't seem to figure out how to properly submit the form with Mechanize. I tried using the Mechanize#click method on the <a> element by passing in the <a>'s href attribute to it, but that hasn't seemed to work either. How do I automate a click on this link (i.e., a submission of the form) properly and/or run the RunSomeScript script in order to submit the form on this redirect page?

2 个答案:

答案 0 :(得分:0)

我不知道你是否或如何获得机械化来支持javascript,但我看到了你可能会看到的其他项目:

Capybara,使用吵闹鬼(phantomjs)驱动程序或watir,也许也使用它的phantomjs支持。

答案 1 :(得分:0)

我认为你可以直接找到表格并用Mechanize提交,即

# if condition checks if the form exists with the id the add some fields to it and then submit    
if page.search("#ctl00_ContentPlaceHolder1_ctrlResults_gvResults_ctl01_lbNext").count > 0
              form = page.forms.first
              form.add_field! "__EVENTTARGET", "ctl00$ContentPlaceHolder1$ctrlResults$gvResults$ctl01$lbNext"
              form.add_field! "__EVENTARGUMENT", ""
              page = form.submit

在您的情况下,您可以使用某个ID找到该表单,或者只使用

form = page.forms.first  
# do whatever you want with this form and then 
page = form.submit