如何使用Ruby中的Mechanize提交表单?

时间:2016-05-10 09:21:13

标签: ruby mechanize

当我运行此代码时:

require 'mechanize'
require 'logger'
require 'nokogiri'
require 'open-uri'

agent = Mechanize.new
agent.log = Logger.new "mech.log"
agent.user_agent_alias = 'Mac Safari'
agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
page = agent.get('https://sarathi.nic.in:8443/nrportal/sarathi/HomePage.jsp')
page = agent.page.links.find{|i| i.text == "Status of Licence"}.click
page.form(:name=>"dlform").field_with(:name=>"dlform:DLNumber").value="TN4020120005045"
page.submit

我收到此错误:

NoMethodError: undefined method `submit' for #<Mechanize::Page:0x8e644cc>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

@sudhagar,您应该提交form而不是page。同时将其分解为单独调用更清晰的代码,如:

...
form = page.form(:name=>"dlform")
form.field_with(:name=>"dlform:DLNumber").value="TN4020120‌​005045"
form.submit 
...