unambiguous XPath expression

Discussions and Tech Support specific to the iMacros Firefox add-on.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
karma
Posts: 6
Joined: Sat May 02, 2009 1:12 pm

unambiguous XPath expression

Post by karma » Mon May 11, 2009 11:09 am

Hi,
does anybody can explain that

'RuntimeError: unambiguous XPath expression:'

Thanks
User avatar
Tech Support
Posts: 4947
Joined: Tue Sep 20, 2005 7:25 pm
Contact:

Re: unambiguous XPath expression

Post by Tech Support » Wed May 27, 2009 11:11 pm

Can you please post a test macro that triggers this error?
Denis, iOpus
Posts: 22
Joined: Mon Apr 14, 2008 2:23 pm

Re: unambiguous XPath expression

Post by Denis, iOpus » Wed Jun 03, 2009 3:52 am

Hi,

I guess you use new XPATH parameter for TAG command. The error appears when the result of XPath evaluation contains more than one element. For example on this web page http://www.iopus.com/imacros/demo/v6/f1/form.asp the following XPath expression

Code: Select all

id('TestForm')/div/center/table/tbody/tr[3]/td
results into three 'td' elements and you should specify the expression further, e.g.

Code: Select all

id('TestForm')/div/center/table/tbody/tr[3]/td[2]
would select the second 'td'.

Denis
connox
Posts: 3
Joined: Thu May 15, 2014 9:19 am

Re: unambiguous XPath expression

Post by connox » Thu May 15, 2014 9:26 am

Hello,

I see this thread is a bit older (found it through the XPATH Wiki page), but I'm having basically the same problem:

The following xpath expression creates a runtime error (ambiguous XPath expression), but I can't see what's wrong with it. It should return exactly one element:

Code: Select all

TAG XPATH="//*[@class='list-products'][1]/a[1]"
Context: my website is an online shop, it contains several product lists (<div class="list-products">) and each of those contains several product links (<a>). This should return the first product link listed on the current page.

Any help appreciated. :)
skippyto
Posts: 108
Joined: Wed Aug 20, 2008 8:14 am

Re: unambiguous XPath expression

Post by skippyto » Thu May 15, 2014 10:15 am

Hi connox !

It won't be easy to help without the html page. If all your div element don't have the same parent node, your expression will probably return to you the first div element for each different parent node, not only the first one.

The extension XPath Checker (https://addons.mozilla.org/fr/firefox/a ... h-checker/) will help you to see the list of all elements returned by your xpath expression so you you'll probably understand where your expression is ambiguous.

This extension is very helpful for me when i work with xpath to target some elements with iMacros.

Hope this help.

Skippyto.
connox
Posts: 3
Joined: Thu May 15, 2014 9:19 am

Re: unambiguous XPath expression

Post by connox » Thu May 15, 2014 10:38 am

Thanks for the tip with the XPath Checker extension, it's really useful!

I found the problem, //*[@class='list-products'][1] actually returns multiple nodes. I didn't think this was possible, but now I know. (The XSLT processor I usually work with behaves differently.)


Edit:

Is this really the intended behaviour?

As far as I know, this:

Code: Select all

//Example[@foo = 'bar'][1]


should NOT be the same as this:

Code: Select all

//Example[@foo = 'bar' and position() = 1]
But unfortunately, the iMacro xpath parser (and also the XPath Checker extension) work this way...

I think multiple predicates should be evaluated one after another, looking at the nodes defined by the previous predicate as a new nodeset, independent from the nodes' positions in the source DOM.


Edit2:
I solved the problem like this:

Code: Select all

//*[@class='list-products' and not(preceding::*[@class='list-products'])]/a[1]
This returns the first <a> node in the first <div class="list-products">

But its not really an elegant solition imho ;)
Post Reply