my dog learned polymorphism*
The moose likes XML and Related Technologies and the fly likes Xpath expression to locate second input tag... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Xpath expression to locate second input tag..." Watch "Xpath expression to locate second input tag..." New topic
Author

Xpath expression to locate second input tag...

Micky Morgan
Greenhorn

Joined: Sep 20, 2006
Posts: 19
Hi,

Could anyone help me in locating the title of the second input tag in the below sample xml document...

I was trying to use //input[@title='Expand Folder'] and it always points to the first input tag...I want to get the second input tag..

I was also trying this which did not work //table/input[1][@title='Expand Folder']

Please note that I can't use names (eg: @name='wcf3ecfcf5c') since they are dynamically generated...

Could any one help me writing the Xpath expression that gets the second input tag...


<html>
.
.


<table

<th><input title="Expand Folder" name="wcf407a583c" src="/testapp/table/expand.gif" type="image">
<input disabled="true" name="wcf3ecfcf5c" type="hidden"><span class="zoom">Folder_A</span>
</th>

<th><input title="Expand Folder" name="wcf947a9541" src="/testapp/table/expand.gif" type="image">
<input disabled="true" name="wcf2f7dc902" type="hidden"><span class="zoom">Folder_B</span>
</th>

</table>

.
.
</html>

Thanks in advance...
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
Try with
//table/input[2][@title='Expand Folder']
or
//table/input[position()=2][@title='Expand Folder']


Position in XPath starts with 1 not with 0. See http://www.w3.org/TR/xpath#predicates
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Micky Morgan:
I was trying to use //input[@title='Expand Folder'] and it always points to the first input tag...
Actually it returns a list that contains both of the input elements. Presumably you are then applying something that selects the first entry from the list.
Could any one help me writing the Xpath expression that gets the second input tag...
And then you want to choose the second entry in that list you already have, so do that: //input[@title='Expand Folder'][2]
Micky Morgan
Greenhorn

Joined: Sep 20, 2006
Posts: 19
Thanks for the prompt response..but unfortunately none of the optios working..

I am actually clicking (thru automation) the image whether it opens or not..
for that I am using syntax like this...

obj.click("//input[@title='Expand Folder']; --> Always clicks Folder_A..

I tried the above options, it returns null (meaning not found)

I think the best option is to try with text since the text is unique..but that also does not work for me..

click("//input[@title='Expand Folder']/text()='Folder_B") ???

Please suggest the XPath expression that matches Folder_B...

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't understand this "clicking" and "automation" business, except when you say "image" it makes me suspect you really want to get the src attribute from that element. And it would help if the example you had posted were well-formed XML. The fact that isn't implies that we aren't looking at the real data and so we are working in the dark.

However I still say the answer is
//input[@title='Expand Folder'][2]
or perhaps
(//input[@title='Expand Folder'])[2]
Micky Morgan
Greenhorn

Joined: Sep 20, 2006
Posts: 19
Thanks to Purushothaman and Paul for prompt responses...

The reason why the above expresions ( //table/input[1] ) did not work because ... table is not immediate parent of input tag as can be seen above...

So the correct XPtah expression that worked for me is..//table/th[2]/input[@title='Expand Folder']

or

//th[2]/input[@title='Expand Folder']


Thanks again for the help...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Xpath expression to locate second input tag...
 
Similar Threads
using ajax and struts iterate tags
Image link in the table - use img or background image ?
Caching rendered html in jsp
jquery accordion is causing jquery treeview picker to breakup
disable image button - Bug in RI implementation?