# Mechanize problem

**URL:** <https://www.ruby-forum.com/t/mechanize-problem/115357>\
**Category:** Ruby\
**Created:** [September 24, 2007, 12:11pm UTC](https://www.ruby-forum.com/t/mechanize-problem/115357 "2007-09-24T12:11:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [September 24, 2007, 12:11pm UTC](https://www.ruby-forum.com/t/mechanize-problem/115357/1 "2007-09-24T12:11:20Z")

</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 == “challenge”}  
form.fields.find{|f| f.name == ‘start’}.value = 1  
page = agent.submit(form, form.buttons.first)  
values = page.body.scan(/**(.+?)\<.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

---

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [September 24, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/mechanize-problem/115357/2 "2007-09-24T12:23:40Z")

</div>

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:

But for the form i want to submit in, the source is:

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?

---

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [September 24, 2007, 1:42pm UTC](https://www.ruby-forum.com/t/mechanize-problem/115357/3 "2007-09-24T13:42:48Z")

</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 == ‘name’}.value = ‘myname’

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

---

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [September 26, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/mechanize-problem/115357/4 "2007-09-26T18:10:15Z")

</div>

On Mon, 2007-09-24 at 20:42 +0900, Lee J. 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’

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

Arlen
