# Problem with Mechanize

**URL:** <https://www.ruby-forum.com/t/problem-with-mechanize/149963>\
**Category:** Ruby\
**Created:** [October 1, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/problem-with-mechanize/149963 "2008-10-01T18:24:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a88e4f/32.png) [@zico](https://www.ruby-forum.com/u/zico)\
**Post date:** [October 1, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/problem-with-mechanize/149963/1 "2008-10-01T18:24:01Z")

</div>

Hi,

I want to use Mechanize to login a sns site.Here is my test code.

require ‘rubygems’  
require ‘mechanize’

class Kaixin  
def initialize(auth, verbose = false)  
@auth = auth  
@verbose = verbose  
@agent = WWW::Mechanize.new  
@agent.redirect\_ok = true  
end

def login

```
page = @agent.get('http://www.kaixin001.com/')

login_form = page.forms.first
login_form.email = @auth['email']
login_form.password = @auth['password']
page1 = @agent.submit(login_form)

pp page1 if @verbose

```

end

end

auth = {‘email’ =\> ‘removed\_email\_address@domain.invalid’,  
‘password’ =\> ‘xxx’}  
kx = Kaixin.new(auth, true)  
kx.login

I use most of the demo code.So i cant see whats the problem.If i type  
the error password,its no page error.if i type the correct pwd,then it  
goes wrong,I find the page use the redirect from “/login” to “/home”  
,does this matter?

Here is the output of netbeans:

c:/ruby/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize/page.rb:118:in  
`bases': undefined method`search’ for nil:NilClass (NoMethodError)

```
    from

```

c:/ruby/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize/chain/uri\_resolver.rb:37:in  
`handle’

```
    from

```

c:/ruby/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize/chain.rb:25:in  
`handle’

```
    from

```

c:/ruby/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize.rb:433:in  
`fetch\_page’

```
    from

```

c:/ruby/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize.rb:509:in  
`fetch\_page’

```
    from

```

c:/ruby/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize.rb:392:in  
`post\_form’

```
    from

```

c:/ruby/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize.rb:324:in  
`submit’

```
    from J:\ROR\RubyProjects\test1\lib/kaixin.rb:20:in `login'

    from J:/ROR/RubyProjects/test1/lib/main.rb:8

```

Can anyone help?thanks!

---

<div class="post-metadata">

**Author:** ![zico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a88e4f/32.png) [@zico](https://www.ruby-forum.com/u/zico)\
**Post date:** [October 23, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/problem-with-mechanize/149963/2 "2008-10-23T18:10:15Z")

</div>

Zhang Zhai å†™é“:

> ```
> @verbose = verbose
> login_form.password = @auth['password']
> 
> ```
> 
> kx.login  
> `bases': undefined method`search’ for nil:NilClass (NoMethodError)  
> c:/ruby/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize.rb:433:in  
> from  
> Can anyone help?thanks!

æ™•ï¼Œä½ ä¹Ÿé‡åˆ°è¿™ä¸ªé—®é¢˜äº†ã€‚æˆ‘çš„é—®é¢˜å’Œä½ ä¸€æ ·ã€‚å‘µå‘µã€‚å‡†å¤‡å¯¹å¼€å¿ƒç½‘ä¸‹æ‰‹~ï¼Ÿ

---

<div class="post-metadata">

**Author:** ![zico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a88e4f/32.png) [@zico](https://www.ruby-forum.com/u/zico)\
**Post date:** [November 1, 2008, 2:54am UTC](https://www.ruby-forum.com/t/problem-with-mechanize/149963/3 "2008-11-01T02:54:44Z")

</div>

ä½ å¥½ï¼Œå‘µå‘µï¼Œæƒ³ç»ƒç»ƒæ‰‹å’¯ã€‚å…¶ä»–ç«™ç‚¹éƒ½å¯ä»¥ï¼Œå°±è¿™ä¸ªä¸è¡Œï¼Œéƒé—·ã€‚

æŽå…‰ wrote:

> Zhang Zhai å†™é“:
> 
> > ```
> > @verbose = verbose
> > login_form.password = @auth['password']
> > 
> > ```
> > 
> > kx.login  
> > `bases': undefined method`search’ for nil:NilClass (NoMethodError)  
> > c:/ruby/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize.rb:433:in  
> > from  
> > Can anyone help?thanks!
> 
> æ™•ï¼Œä½ ä¹Ÿé‡åˆ°è¿™ä¸ªé—®é¢˜äº†ã€‚æˆ‘çš„é—®é¢˜å’Œä½ ä¸€æ ·ã€‚å‘µå‘µã€‚å‡†å¤‡å¯¹å¼€å¿ƒç½‘ä¸‹æ‰‹~ï¼Ÿ
