how can we click an image button

46 views
Skip to first unread message

Parul

unread,
Dec 30, 2008, 4:14:20 AM12/30/08
to Watir General
Hi all,
Can anyone tell me how we can click an image in ruby for watir.

I used the below syntax:
ie.image(:src, "xyz.gif").click

Is it correct??

Thanks
Parul

anna barbara ostrowska

unread,
Dec 30, 2008, 7:19:14 AM12/30/08
to watir-...@googlegroups.com
Hi there!
 
 For example:

ie.image(:id, 'x').fire_event('onclick')

2008/12/30 Parul <paru...@gmail.com>

srinivas subramanian

unread,
Dec 30, 2008, 1:52:40 PM12/30/08
to watir-...@googlegroups.com
Hi,
The method used by Parul should also work. But, only thing is you have to mention the entire path of the image file.

For eg:
ie.image(:src, "C:\Desktop\xyz.gif").click

Note: We can also bring in to use some other attributes of the image like id, name etc. if applicable.

Thanks,
Srinivas.

Charley Baker

unread,
Dec 30, 2008, 1:58:48 PM12/30/08
to watir-...@googlegroups.com
Your image may also be a button, and you don't have to use the entire path if you use a regular expression: 

browser.button(:src, /foo.gif/).click


Charley Baker
blog: http://charleybakersblog.blogspot.com/
Project Manager, Watir, http://wtr.rubyforge.org
QA Architect, Gap Inc Direct

srinivas subramanian

unread,
Dec 30, 2008, 3:12:14 PM12/30/08
to watir-...@googlegroups.com
Ya very true. Regular expressions helps us a lot while scripting. Its just awesome. But, the method which Parul had mentioned, requires us to specify the entire path.

Wilson Xu

unread,
Dec 30, 2008, 8:34:19 PM12/30/08
to watir-...@googlegroups.com
Refer to Image API:
image(how,what=nil)

This is the main method for accessing images - normally an <img src="image.gif"> HTML tag.

 *  how   - symbol - how we access the image, :index, :id, :name, :src, :title or :alt are supported
 *  what  - string, integer or regular expression - what we are looking for,

Valid values for 'how' are listed in the Watir Wiki - wiki.openqa.org/display/WTR/Methods+supported+by+Element

returns an Image object

Typical Usage

  ie.image(:src, /myPic/)             # access the first image that matches myPic. We can use a string in place of the regular expression
                                      # but the complete path must be used, ie.image(:src, 'http://myserver.com/my_path/my_image.jpg')
  ie.image(:index,2)                  # access the second image on the page
  ie.image(:alt, "A Picture")         # access an image using the alt text
  ie.image(:xpath, "//img[@alt='A Picture']/")    # access an image using the alt text

Reply all
Reply to author
Forward
0 new messages