Archive

Posts Tagged ‘watir xpath’

watir div content by class

December 11th, 2009 dwright No comments

Here are 3 identical ways to capture the content in a div by only the class name.

<div class="error">error message here</div>

@ie.div(:class, 'error').text
@ie.div(:class, 'error').innerText
@ie.element_by_xpath("//div[@class='error']").text

all the above return 'error message here'

Categories: watir Tags:

watir advanced xpath onclick example

December 7th, 2009 dwright 3 comments

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: