Forum Discussion

longchasen's avatar
longchasen
Contributor
5 years ago

Xpath Get last matching result

I have an xpath expression like this: 

 

//td[@div='id']/nobr[text()='Total AR:']

 

Upon checking on the inspector element there are multiple matches, both on /td[@div='id'] and nobr[text()='Total AR:']. I only need to get the last result as im sure thats what i need. Basically im trying to do something like this:

 

//td[@div='id'][last()]/nobr[text()='Total AR:']

 

how can i get the last result ?

 

  • Perhaps use the array return of xpath query.

    And take the last one.

     

    objectfind = MyPage.contentDocument == null ? null : MyPage.FindChildByXPath(toFind);
    if ((typeof objectfind != 'undefined') && (objectfind != null)) {
      objectfind = (new VBArray(objectfind)).toArray();
      return (objectfind[objectfind.length-1]);
    }
    else
      return null;
  • BenoitB's avatar
    BenoitB
    Community Hero

    Perhaps use the array return of xpath query.

    And take the last one.

     

    objectfind = MyPage.contentDocument == null ? null : MyPage.FindChildByXPath(toFind);
    if ((typeof objectfind != 'undefined') && (objectfind != null)) {
      objectfind = (new VBArray(objectfind)).toArray();
      return (objectfind[objectfind.length-1]);
    }
    else
      return null;