tags:

views:

67

answers:

1

I'm trying to write xpath for one of the elements in a tree-like structure in the UI.

The tree looks like a windows file structure, like parent node, child. So, in order to find child node parent node has to be clicked.

+ [file icon] Book
|_ Book 1
|_ Book 2
|_ Book 3

Selenium gives the following xpath for the text 'Book' in above tree shown

//ul[@id='book_xxx']/li/ul/li[8]/span

when I click on file icon, selenium gave me following

//ul[@id='book_xxx']/li/ul/li[8]/img[1]

How can one write an xpath for clicking the file icon(i.e image) based on knowing the span text? I need the xpath for clicking on file icon image.

A: 

It appears that the span and img elements are both children of the 8th li element, and I'm assuming the text inside the span that you would like to match on is "Book".

If you wanted to select the span filtering on the text you should be able to use:

//ul[@id='book_xxx']/li/ul/li[span[text()='Book']]/img[1]

This uses a nested predicate filter to identify the li that has a child span element who's text node value is "Book", and then selects that li's first img element.

Mads Hansen