0

This is the base script I'm trying to change

http://jsfiddle.net/gaby/hsv97/2/

It gives the xpath to the clicked element. When you click with deeper it returns

//html/body/div[2]/em#emphatic/span#deepest.red

That's the complete path to the element. But if the webmaster decides to put in an extra div, then the path to #deepest.red gets broken. What i want is the shortest possible path, calculated from the nearest id . So in this case, I only need #deepest.red which i later can use to address this element again with

$x("//div[contains(@id,'deepest-red')]");

Can someone point me in the right direction to change this fiddle to fit my needs.......

1
  • Why //div[contains(@id,'deepest-red')]? Shouldn't it be //div[@id='deepest-red']? Dec 28, 2013 at 11:06

1 Answer 1

0

Not sure I fully understood your request, but if you need to solve the problem of an extra <div> in the path, then the 'descendant-or-self' axis.

descendant-or-self
Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself

So your XPath will look like below:

//html/body/descendant-or-self::div/em#emphatic/span#deepest.red

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.