# Mechanize problem

**URL:** <https://rubytalk.org/t/mechanize-problem/40867>\
**Category:** ruby-talk\
**Created:** [24 September 2007 10:11 UTC](https://rubytalk.org/t/mechanize-problem/40867 "2007-09-24T10:11:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/l/82dd89/32.png) [@Lee\_Jarvis](https://rubytalk.org/u/Lee_Jarvis)\
**Post date:** [24 September 2007 10:11 UTC](https://rubytalk.org/t/mechanize-problem/40867/1 "2007-09-24T10:11:24Z")

</div>

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](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](http://www.the-c0re.org/challenges/1/index.php)")  
form = site.forms.find{|f| [f.name](http://f.name) == "challenge"}  
form.fields.find{|f| [f.name](http://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](http://f.name) == "challenge"}  
form.fields.find{|f| [f.name](http://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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Lee\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/l/82dd89/32.png) [@Lee\_Jarvis](https://rubytalk.org/u/Lee_Jarvis)\
**Post date:** [24 September 2007 10:23 UTC](https://rubytalk.org/t/mechanize-problem/40867/2 "2007-09-24T10:23:41Z")

</div>

Ok well i think its because i am using

form.fields.find{|f| [f.name](http://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](http://f.name) == 'answer'}.value

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Lee\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/l/82dd89/32.png) [@Lee\_Jarvis](https://rubytalk.org/u/Lee_Jarvis)\
**Post date:** [24 September 2007 11:42 UTC](https://rubytalk.org/t/mechanize-problem/40867/3 "2007-09-24T11:42:59Z")

</div>

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](http://f.name) == 'name'}.value = 'myname'

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Arlen\_Christian\_Mar1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Arlen\_Christian\_Mar1](https://rubytalk.org/u/Arlen_Christian_Mar1)\
**Post date:** [26 September 2007 16:08 UTC](https://rubytalk.org/t/mechanize-problem/40867/4 "2007-09-26T16:08:29Z")

</div>

Can you tell us what the value of form.fields.find{|f| f.name == 'name'}  
is at this stage?

Arlen

> **···**
>
> On Mon, 2007-09-24 at 20:42 +0900, Lee Jarvis wrote:
> 
> > 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'
