Gem [已解决] 关于 Mechanize::Form::Field 选择 Form Field 的问题 (实现表单自动提交)

tylr · 2012年05月09日 · 最后由 tylr 回复于 2012年05月09日 · 3268 次阅读

Mechanize::Form::Field 文档链接: http://mechanize.rubyforge.org/Mechanize/Form/Field.html

agent = Mechanize.new
page = agent.get("http://localhost/NewTask.aspx")
#获取目标page
submit_form = page.form("aspnetForm")
#通过form name获取目标form

完成如上这几步后返回了页面:
=> #<Mechanize::Form
 {name "aspnetForm"}
 {method "POST"}
 {action "NewTask.aspx"}
  [text:0xacef3c type: text name: ctl00$ContentPlaceHolder1$txtNumber value: ]
  [text:0xacec78 type: text name: ctl00$ContentPlaceHolder1$txtTime1 value: 2012-05-09 14:02:17]
  [textarea:0xacd880 type:  name: ctl00$ContentPlaceHolder1$txtValue1 value:]
  [selectlist:0xa9c278 type:  name: ctl00$ContentPlaceHolder1$cmbPriority value: []]
...




我尝试了用 submit_form.field.和 submit_form.去获取 field 并希望将其赋值都失败了,报错如下,请问我如何才能定位到这些 field 呢?

irb(main):011:0> submit_form.ctl00$ContentPlaceHolder1$txtNumber = 1000
SyntaxError: (irb):11: syntax error, unexpected tGVAR, expecting $end
submit_form.ctl00$ContentPlaceHolder1$txtNumber = 1000
                                               ^
        from C:/Ruby193/bin/irb:12:in `<main>'




submit_form["ctl00$ContentPlaceHolder1$txtNumber"] = 1000

这样试试呢!

#1 楼 @huacnlee 太感谢你啦,貌似成了,我一运行这段代码就在 irb 返回了 => 1000 想想也是,我之前怎么会直接用 form.field_name 去访问呢,又不是 method,受教了受教了!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号