# Newb question. Undefined Method error

**URL:** <https://rubytalk.org/t/newb-question-undefined-method-error/64646>\
**Category:** ruby-talk\
**Created:** [22 January 2012 00:17 UTC](https://rubytalk.org/t/newb-question-undefined-method-error/64646 "2012-01-22T00:17:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_K](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@Daniel\_K](https://rubytalk.org/u/Daniel_K)\
**Post date:** [22 January 2012 00:17 UTC](https://rubytalk.org/t/newb-question-undefined-method-error/64646/1 "2012-01-22T00:17:16Z")

</div>

So I'm trying to run this script

> **···**
>
> -------------------------------------------------  
> #!/usr/bin/env ruby  
> #  
> #sample ruby script using the selenium client  
> #  
> require "rubygems"  
> gem "selenium-webdriver", "\>=2.17.0"  
> require "selenium/webdriver"  
> begin  
> @browser = Selenium::Client::Driver.new\  
> &nbsp;&nbsp;:host =\> "localhost",  
> &nbsp;&nbsp;:port =\> 4444,  
> &nbsp;&nbsp;:browser =\> "\*firefox",  
> &nbsp;&nbsp;:url =\> "[http://www.google.com](http://www.google.com)",  
> &nbsp;&nbsp;:timeout\_in\_second =\> 60  
> @browser.start\_new\_browser\_session  
> &nbsp;&nbsp;&nbsp;@browser.open "/"  
> &nbsp;&nbsp;&nbsp;@browser.type "q", "Selenium [seleniumhq.org](http://seleniumhq.org)"  
> &nbsp;&nbsp;&nbsp;@browser.click "btnG", :wait\_for =\> :page  
> &nbsp;&nbsp;&nbsp;puts @browser.text? ("[seleniumhq.org](http://seleniumhq.org)")  
> ensure  
> &nbsp;&nbsp;@browser.close\_current\_browser\_session  
> end  
> -------------------------------------------------
> 
> And I get this response when I run it.
> 
> seleniumtest\_script.rb:24:in `ensure in \<main\>': undefined method  
> `close\_current\_browser\_session' for nil:NilClass (NoMethodError)  
> &nbsp;&nbsp;from seleniumtest\_script.rb:24:in `\<main\>'
> 
> So my question is, what am I missing?  
> Thanks in advance.
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Dave\_Aronson1](https://avatars.discourse-cdn.com/v4/letter/d/8e7dd6/32.png) [@Dave\_Aronson1](https://rubytalk.org/u/Dave_Aronson1)\
**Post date:** [22 January 2012 00:33 UTC](https://rubytalk.org/t/newb-question-undefined-method-error/64646/2 "2012-01-22T00:33:41Z")

</div>

> So I'm trying to run this script

[snip]

> And I get this response when I run it.
> 
> seleniumtest\_script.rb:24:in `ensure in \<main\>': undefined method  
> `close\_current\_browser\_session' for nil:NilClass (NoMethodError)  
> from seleniumtest\_script.rb:24:in `\<main\>'
> 
> So my question is, what am I missing?

Assuming that what you've posted is indeed seleniumtest\_script.rb, and  
that line 24 is indeed the one where you do  
"@browser.close\_current\_browser\_session" (even though you have fewer  
than 24 lines of code there!), @browser is nil. Any time you see  
"undefined method `some\_method' for nil:NilClass" that means that  
you've tryied to call some\_method on an object that's actually nil.

Now the question is, why is it nil? Most likely your call to  
"@browser = Selenium::Client::Driver.new" (with the args on further  
lines) is failing.

So now the question is, why is that failing? That is left as an  
exercise for the reader. 😉

-Dave

> **···**
>
> On Sat, Jan 21, 2012 at 19:17, Daniel K. \<tswbyaj@gmail.com\> wrote:
> 
> --  
> Dave Aronson, President, Dave Aronson Software Engineering and Training  
> Ruby on Rails Freelancing (Northern Virginia, Washington DC, or Remote)  
> DaveAronson.com, Codosaur.us, Dare2XL.com, & RecruitingRants.com (NEW!)  
> Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (me)
