Forum: Ruby Watir: Access an Object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
97334b75c93574ed3e514f33849f0953?d=identicon&s=25 Mario Ruiz (tcblues)
on 2009-01-13 12:28
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.
Ff97ca87af59ee68ceff5877a8365788?d=identicon&s=25 Jarmo Pertman (juuser)
on 2009-01-14 15:45
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"

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 Programming Language") # outputs true

# this only works with Watir 1.6.x+ versions
puts b.element(:text => "Ruby Programming Language", :index => 1).href #
multiple attributes, e.g. first link with specified text

Best regards,
Jarmo

Mario Ruiz 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
E8d97590e9c83b09f52984d89d66801e?d=identicon&s=25 Michael Michael (rubyfinder)
on 2009-01-14 17:07
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,
mmartino@apexsystemsinc.com 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 Ruiz 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.
This topic is locked and can not be replied to.