Mechanize problem

L

Lee Jarvis

Ok guys, ignore the ugly code. i just want this to work before i build
it up. So it is very ugly and well, all round bad.. But i'll work on
that after.

I am using WWW::Mechanize and i'm trying to do a challenge (that i
actually created).. here is the challenge:
http://www.the-c0re.org/challenges/1/index.php

I have written code to submit the first POST data and start the
challenge, i have written code to get the values from the XML file.. But
i can't seem to post these values in the original form.. You may have a
better idea if you look at it.. Sorry about the code (like i said, i
just want it to work)

This is what i have so far:

require 'rubygems'
require 'mechanize'

agent = WWW::Mechanize.new
site = agent.get("http://www.the-c0re.org/challenges/1/index.php")
form = site.forms.find{|f| f.name == "challenge"}
form.fields.find{|f| f.name == 'start'}.value = 1
page = agent.submit(form, form.buttons.first)
values = page.body.scan(/<b>(.+?)<.b>/)
link = page.body.scan(/href="(.+?)">/)
xml = agent.get link
arr = Array.new
values.each {|v| arr.push(xml.body.scan(/<#{v}>(.+?)<\/#{v}>/)) }
form = page.forms.find{|f| f.name == "challenge"}
form.fields.find{|f| f.name == 'answer'}.value = arr.join(' ')
newpage = agent.submit(form, form.buttons.first)

this is the error i get:
undefined method `value=' for nil:NilClass (NoMethodError)

I don't see why though, the variable page holds the page with the
correct form data and correct form names, So i have no clue.. Sorry if i
haven't explained myself enough..

Thanks for anything you can give
 
L

Lee Jarvis

Ok well i think its because i am using

form.fields.find{|f| f.name == 'answer'}.value

Which works for submit, because the source is:
<input name="start" type="hidden" id="start" value="1">

But for the form i want to submit in, the source is:
<input name="answer" type="textfield" id="answer">

So im guessing
form.fields.find{|f| f.name == 'answer'}.value

Won't work, because there is no value part, Does this mean that i can't
submit into the form?
 
L

Lee Jarvis

Bump, Ok i have cleaned up my code a bit, I have read through ALL of the
docs, i have seen many examples.. and EVERY example uses:
form.fields.find{|f| f.name == 'name'}.value = 'myname'

Unless i am doing something else wrong them it seems almost impossible
to do this
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top