Xpath - node search limit not working?

What am I doing wrong here? I'm trying to limit my xpath search to a specific row in my table, but my query always returns the contents of the range on the first row:

var query = "//span[contains(@id, 'timer')]";
var root = document.getElementById('movements').rows[1];
document.evaluate(query, root, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.textContent

      

Please, help!

+2


a source to share


3 answers


"//span[contains(@id, 'timer')]"

      

Problem : This is an absolute XPath expression and ignores the fact that you are evaluating it from a specific string.



Solution . To achieve the desired result use:

.//span[contains(@id, 'timer')]

+2


a source


You get the first line:

var root = document.getElementById('movements').rows[1];

      

This will get the second line:



var root = document.getElementById('movements').rows[2];

      

You need to provide the correct index of the desired row.

0


a source


Try this instead:

var query = "//td//span[contains(@id, 'timer')]";

      

0


a source







All Articles