# Watir: Access an Object

**URL:** <https://www.ruby-forum.com/t/watir-access-an-object/157591>\
**Category:** Ruby\
**Created:** [January 13, 2009, 12:28pm UTC](https://www.ruby-forum.com/t/watir-access-an-object/157591 "2009-01-13T12:28:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [January 13, 2009, 12:28pm UTC](https://www.ruby-forum.com/t/watir-access-an-object/157591/1 "2009-01-13T12:28:43Z")

</div>

I need to access an object with Watir through the name without knowing  
what kind of object is.  
For example the control could be a button or a link and  
I would like to do something like:

require ‘watir’  
include Watir  
ie=IE.new  
ie.goto(“file://c:/temp2.htm”)

ie.object(:name,“imp1”).click()

Is there any way???  
Thank you in advance.

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [January 14, 2009, 3:45pm UTC](https://www.ruby-forum.com/t/watir-access-an-object/157591/2 "2009-01-14T15:45:18Z")

</div>

Okay. I did something like this. Downside of this implementation is that  
it iterates through all elements before finding the correct one. So if  
you’d search for text\_field, then first it would search for a button,  
then for a link and just after that text\_field. You can of course change  
the sequence to be something else and to include some other elements.  
Also, please bear in mind, that first element, which exist with these  
attributes will be returned.

I’d suggest to use b.text\_field(:blah, blah) instead of .element in the  
future anyway, but I’ll provide here one working example:

require ‘watir’

class Watir::IE # monkey-patch Watir’s IE class  
def element \*args  
arg1, arg2 = args # this is needed so hashes would work as input  
paremeters as with regular Watir  
elements = [:button, :link, :text\_field] # Watir elements which will  
be searched

```
elements.each do |el| # iterate over each element
  element = self.send(el, arg1, arg2)

  return element if element.exists? # return element if it exists on 

```

page  
end  
nil # return nil if element was not found  
end  
end

b = Watir::IE.new  
b.goto “[http://www.google.com](http://www.google.com)”

google\_text\_field = b.element(:name, “q”) # access text\_field with two  
parameters

google\_button = b.element(:name, “btnG”) # access it with two parameters

p google\_text\_field.class # outputs Watir::TextField  
p google\_button.class # outputs Watir::Button

google\_text\_field.value = “ruby” # type “ruby” into search string text  
field  
google\_button.click # and click search

puts b.text.include?(“Ruby P.ming Language”) # outputs true

# this only works with Watir 1.6.x+ versions

puts b.element(:text =\> “Ruby P.ming Language”, :index =\> 1).href #  
multiple attributes, e.g. first link with specified text

Best regards,  
Jarmo

Mario R. wrote:

> I need to access an object with Watir through the name without knowing  
> what kind of object is.  
> For example the control could be a button or a link and

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [January 14, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/watir-access-an-object/157591/3 "2009-01-14T17:07:30Z")

</div>

I don’t mean to be too intrusive as I am a dreadful IT Recruiter that  
you probably all despise. I am working on filling a number of RoR  
Developer positions for an exciting young company in NYC. If you are  
interested in learning more about the position please e-mail me,  
removed\_email\_address@domain.invalid or call me at 212.991.1835. No one has to  
know that you contacted me regarding and you can still bash me here in  
the blog.

Have a great day! Thanks!

Mario R. wrote:

> I need to access an object with Watir through the name without knowing  
> what kind of object is.  
> For example the control could be a button or a link and  
> I would like to do something like:
> 
> require ‘watir’  
> include Watir  
> ie=IE.new  
> ie.goto(“file://c:/temp2.htm”)
> 
> ie.object(:name,“imp1”).click()
> 
> Is there any way???  
> Thank you in advance.
