# Simple problem w/ WWW::Mechanize

**URL:** <https://www.ruby-forum.com/t/simple-problem-w-www-mechanize/90924>\
**Category:** Ruby\
**Created:** [April 3, 2007, 1:00am UTC](https://www.ruby-forum.com/t/simple-problem-w-www-mechanize/90924 "2007-04-03T01:00:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 3, 2007, 1:00am UTC](https://www.ruby-forum.com/t/simple-problem-w-www-mechanize/90924/1 "2007-04-03T01:00:59Z")

</div>

Hi all, I’m fairly new to Ruby, and, as an exercise, I’m writing some  
code for webforms. However, I can’t seem to get the following code to  
work - instead, I get the following error:  
wikitesting.rb:8:in `get': undefined method `myform’ for  
#WWW::Mechanize::Page:0xb7996738 (NoMethodError)  
from wikitesting.rb:10

Probably pretty simple, and I can see it’s something wrong with the  
way I called the function, however, I’ve spent some time on it and  
can’t figure it out. Here’s the code:  
require ‘rubygems’  
require ‘mechanize’  
def get(pagename)  
agent = WWW::Mechanize.new  
agent.user\_agent = ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT  
5.1)’  
page = agent.get(‘[Wikipedia, the free encyclopedia](http://en.wikipedia.org/w/index.php?title=)’ +  
pagename + ‘&action=edit’)  
myform = page.form(‘editform’)  
return page.myform.fields.form(‘wpTextbox1’)  
end  
text = get(‘Computer’)  
pp text

Thanks,

Tim

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 3, 2007, 1:26am UTC](https://www.ruby-forum.com/t/simple-problem-w-www-mechanize/90924/2 "2007-04-03T01:26:00Z")

</div>

Hi Tim,

On Tue, Apr 03, 2007 at 08:00:15AM +0900, removed\_email\_address@domain.invalid  
wrote:

> require ‘rubygems’  
> require ‘mechanize’  
> def get(pagename)  
> agent = WWW::Mechanize.new  
> agent.user\_agent = ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT  
> 5.1)’  
> page = agent.get(‘[Wikipedia, the free encyclopedia](http://en.wikipedia.org/w/index.php?title=)’ +  
> pagename + ‘&action=edit’)  
> myform = page.form(‘editform’)

Try replacing this line:

> return page.myform.fields.form(‘wpTextbox1’)

With this:

return myform.wpTextbox1

You’ve already found the form and stored it in the ‘myform’ variable.

> end  
> text = get(‘Computer’)  
> pp text

Also, if you have more questions, be sure to join the mechanize mailing  
list:

[http://rubyforge.org/mailman/listinfo/mechanize-users](http://rubyforge.org/mailman/listinfo/mechanize-users)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 3, 2007, 2:26am UTC](https://www.ruby-forum.com/t/simple-problem-w-www-mechanize/90924/3 "2007-04-03T02:26:04Z")

</div>

On Apr 2, 7:25 pm, Aaron P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > from wikitesting.rb:10
> > 
> > ```
> > 
> > page = agent.get('[Apostrophe - Wikipedia](http://en.wikipedia.org/w/index.php?title='+)
> 
> –  
> Aaron P.[http://tenderlovemaking.com/](http://tenderlovemaking.com/)

Thanks for replying. I still get a similar error, though:  
wikitesting.rb:8:in `get': undefined method `wpTextbox1’ for  
nil:NilClass (NoMethodError)  
from wikitesting.rb:10

Any ideas? By the way, I’ve joined the mailing list, and any future  
questions I might have will probably be directed there.

Thanks,

Tim

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 4, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/simple-problem-w-www-mechanize/90924/4 "2007-04-04T22:06:06Z")

</div>

On Apr 3, 1:36 am, Aaron P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> change ‘Computer’ to ‘Main’, it seems to work:  
> myform = page.form(‘editform’)  
> return myform.wpTextbox1  
> end  
> text = get(‘Main’)  
> pp text
> 
> –  
> Aaron P.[http://tenderlovemaking.com/](http://tenderlovemaking.com/)

OK, thanks, that seems to work!

Tim

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 3, 2007, 7:37am UTC](https://www.ruby-forum.com/t/simple-problem-w-www-mechanize/90924/5 "2007-04-03T07:37:21Z")

</div>

On Tue, Apr 03, 2007 at 08:50:04AM +0900, removed\_email\_address@domain.invalid  
wrote:

> Thanks for replying. I still get a similar error, though:  
> wikitesting.rb:8:in `get': undefined method `wpTextbox1’ for  
> nil:NilClass (NoMethodError)  
> from wikitesting.rb:10
> 
> Any ideas? By the way, I’ve joined the mailing list, and any future  
> questions I might have will probably be directed there.

It looks like Wikipidia may not like that parameter in the URL. When I  
change ‘Computer’ to ‘Main’, it seems to work:

require ‘rubygems’  
require ‘mechanize’

def get(pagename)  
agent = WWW::Mechanize.new  
agent.user\_agent = ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT  
5.1)’  
page = agent.get(‘[Wikipedia, the free encyclopedia](http://en.wikipedia.org/w/index.php?title=)’ +  
pagename + ‘&action=edit’)

```
myform = page.form('editform')
return myform.wpTextbox1

```

end  
text = get(‘Main’)  
pp text
