Using xpath to click a button after text

688 views
Skip to first unread message

Usha Loutongbam

unread,
Mar 28, 2009, 5:27:05 PM3/28/09
to watir-...@googlegroups.com
Hi,

The truncated version of the HTML source code I have looks something like this:

<tbody>
<tr>       </tr>
<tr>       
      <div>
      <td><div><span class="logo" style="background-image: url(/images/logos/ab.gif);"></span>
     <span class="pc_name">Apple</span></div></td>
      <td>   </td>
                      <td>   </td>.............
      <td><div><input type="image" class="book-btn" title="Book this" alt="Book this" src="/images/buttons/book.gif" tripid="7"/>
...................
<tbody>
<tr>...............................

I'm looking for the first instance of the text "Apple" or the image file "ab.gif" which could be in multiple <tbody>. I then want to click on a book button (book-btn) on the same row as this text found. The text is always on first cell and the book button is always on the 7th cell.

So far, I haven't been able to do so using xpath on FireWatir 1.6.2. Tried some examples given by Angrez and others, but it still is not working:

ff.button(:xpath, "//span[@text='Apple']/../td[7]/div/input").click()
ff.button(:xpath, "//[contains(text(),'Apple')]/../td[7]/input").click() 

Both give an error saying "Unable to locate element, using :xpath...". Am I doing something wrong here? 

Any help appreciated!


Thanks,
Usha

Angrez Singh

unread,
Mar 30, 2009, 2:15:50 AM3/30/09
to watir-...@googlegroups.com
I think "//span[@text='Apple']/../" points to "div" tag because div is parent of that span. May be you can try
"//span[@text='Apple']/../../../../" it will point to "div/td/div/tr" and then you can do "td[7]"

Let me know if that works. I guessed this from the HTML you send.

- Angrez

Usha Loutongbam

unread,
Mar 30, 2009, 8:56:36 AM3/30/09
to watir-...@googlegroups.com
After making small changes for the path (you suggested) and the attribute, it worked: ff.button(:xpath, "//span[text()='Apple']/../../../td[7]/div/input").click()

For some reason it is not working with "@text". If I replace it with "text()" it works.

Thanks for the help!

Angrez Singh

unread,
Mar 30, 2009, 9:52:46 AM3/30/09
to watir-...@googlegroups.com
This is because text in not "attribute" of element its a "property".

- Angrez
Reply all
Reply to author
Forward
0 new messages