Home > ruby, watir > watir advanced xpath onclick example

watir advanced xpath onclick example

problem space:
you are trying to click a (click-able) link element, with no real discerning qualities. (i.e. there is no name, no id, a generic css class same as the other links on the page, etc. Luckily, In this case there is a javascript onclick handler which is unique to this element.

<a class="showhide" href="javascript:void(0)" onclick="setDisplay('notes','block');">show

you could accomplish this two ways. (that I know of)
1. find the number of the link in relation to the page, (i.e. it's the 13th)
ie.link(:index, 13).click
2. specify an xpath.
ie.link(:xpath, "//a[contains(@onclick, \"setDisplay('notes','block')\")]").click

(I just also, 'discovered' that firebug (Firefox JavaScript debugging extension) offers an xpath to elements, (in the right click). it offered this: (untested)
/html/body/div[2]/table/tbody/tr/td/table/tbody/tr/td/div/form/table/tbody/tr/td/div[2]/table[7]/tbody/tr[3]/td/a

ref: groups.google.com/watir-general

Categories: ruby, watir Tags:
  1. AJ
    February 4th, 2010 at 23:47 | #1

    I was looking around because I also have a page with a bunch of href="javascript:void(0)" buttons, and watir has this ability

    Button code -

    (All the buttons went to different sites)

    This code got me what I needed
    ie.button(:value, "Click Me!").fire_event("onclick")

  2. AJ
    February 4th, 2010 at 23:48 | #2

    @AJ

    Button Code:
    input type=text value="Click Me!" onClick="redirect_user('http://google.com')"

  3. February 5th, 2010 at 10:38 | #3

    @AJ
    Thanks for posting, I haven't used 'fire_event', good to keep in mind