Using xpath to find Nth instance of desired value

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
spar108r
Newbie
Posts: 13
Joined: Tue Oct 24, 2006 12:10 am
Location: Toronto, Ontario, Canada

Using xpath to find Nth instance of desired value

Post by spar108r » Thu Mar 09, 2023 2:45 pm

Hello,

I have the following HTML source snippet yielded on a page after loading with the built-in web browser functions for Edge.

Code: Select all

<table>
  <tbody>
    <tr bgcolor="#AAAAAA">
    <tr>
    <tr>
    <tr>
    <tr>
      <td>Color Digest </td>
      <td>AgArAQICGQMVBBwTIRQHIwg0GUMURAZTBWQJcwV0AoEDAQ</td>
    </tr>
    <tr>
      <td>Color Digest </td>
      <td>2,43,2,25,21,28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,33,7</td>
    </tr>
  </tbody>
</table>
I'm trying to locate the 2nd td instance that contains "Color Digest" or equals "Color Digest ", then get the value from the immediate next td following it. So, in the case above...it would select this value "2,43,2,25,21,28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,33,7".

I believe xpath is the way to go, however I can't figure out the correct syntax to represent this that Macro Scheduler will accept :D

Any guidance is greatly appreciated.

Many Thanks,

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1348
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Using xpath to find Nth instance of desired value

Post by Dorian (MJT support) » Thu Mar 09, 2023 4:05 pm

You can do with the "tag name" strategy and look through the table rows.

Code: Select all

EdgeFindElements>session_id,tag name,tr,el

let>foundnum=0
Let>ExtractLoop=0
repeat>ExtractLoop
  Let>ExtractLoop=ExtractLoop+1
  EdgeGetElementData>session_id,el_%ExtractLoop%,text,thevalue
  pos>Color Digest,thevalue,1,poscol

  if>poscol>0
  let>foundnum=foundnum+1
    If>foundnum=2
    StringReplace>thevalue,Color Digest ,,thevalue
    put>thevalue
    MDL>thevalue
    Endif
  endif
  Until>ExtractLoop,el_count

Yes, we have a Custom Scripting Service. Message me or go here

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Using xpath to find Nth instance of desired value

Post by hagchr » Thu Mar 09, 2023 4:38 pm

Hi, alternative using xPath

Code: Select all

Let>EDGEDRIVER_EXE=C:\Users\Christer\Desktop\EdgeDriver\msedgedriver.exe
EdgeStart>strSessionID

Let>tmpURL=file:///C:/Users/Christer/Desktop/Test.html
EdgeNavigate>strSessionID,url,tmpURL

// Two examples of xPath
Let>strXPATH=//tr[contains(.,'Color')][2]//td[2]
//Let>strXPATH=//tr[contains(.,'Color')][1]/following-sibling::tr/td[2]

EdgeFindElements>strSessionID,xpath,strXPATH,strElementID
EdgeGetElementData>strSessionID,strElementID_1,text,strResult

MDL>strResult

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1348
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Using xpath to find Nth instance of desired value

Post by Dorian (MJT support) » Thu Mar 09, 2023 4:52 pm

hagchr wrote:
Thu Mar 09, 2023 4:38 pm
Hi, alternative using xPath

Code: Select all

Let>EDGEDRIVER_EXE=C:\Users\Christer\Desktop\EdgeDriver\msedgedriver.exe
EdgeStart>strSessionID

Let>tmpURL=file:///C:/Users/Christer/Desktop/Test.html
EdgeNavigate>strSessionID,url,tmpURL

// Two examples of xPath
Let>strXPATH=//tr[contains(.,'Color')][2]//td[2]
//Let>strXPATH=//tr[contains(.,'Color')][1]/following-sibling::tr/td[2]

EdgeFindElements>strSessionID,xpath,strXPATH,strElementID
EdgeGetElementData>strSessionID,strElementID_1,text,strResult

MDL>strResult
Aah, love it! Thank you.
Yes, we have a Custom Scripting Service. Message me or go here

spar108r
Newbie
Posts: 13
Joined: Tue Oct 24, 2006 12:10 am
Location: Toronto, Ontario, Canada

Re: Using xpath to find Nth instance of desired value

Post by spar108r » Thu Mar 09, 2023 6:04 pm

This is perfect.
Thanks Gents!!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts