Capturing text of its corresponding targeted heading of a table

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
uniadv
Junior Coder
Posts: 29
Joined: Tue Aug 04, 2020 2:16 am

Capturing text of its corresponding targeted heading of a table

Post by uniadv » Fri Aug 14, 2020 5:43 am

Hi Team,
I have been working on capturing the data from table in Chrome depending upon its heading text. In this table "Column 1" contains the heading which is fixed but its position changes throughout the table whenever i run the code. Corresponding to the heading text in "Column 1", its data present in the "Column 2" which is variable.

I am trying capture only Column 2 data depending upon its column 1 value. I figured out that it can be done using "ArrayFind" but having difficulty to understanding its implementation.

Image

If the targeted heading is at:

Code: Select all

/html/body/div[3]/div[3]/div[5]/div[1]/table[3]/tbody/tr[3]/td[1]
then i want data from:

Code: Select all

/html/body/div[3]/div[3]/div[5]/div[1]/table[3]/tbody/tr[3]/td[2]
but first it need to find at which row, that particular heading is present whose data need to be captured.
Any help how can i proceed with that?

Regards

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

Re: Capturing text of its corresponding targeted heading of a table

Post by Dorian (MJT support) » Fri Aug 14, 2020 10:38 am

I'm not sure ArrayFind would help you here, as the data returned by ChromeFindElements isn't what you're looking for until you've used ChromeGetElementData - so you have to loop through the ChromeFindElements array anyway.

Also, according to the helpfile for ArrayFind -"Stops at the first matching index and returns the index of the item in result". This means it would only find the first result anyway, not all of them if there happened to be more than 1.

The code below will extract table rows, then check to see if they contain your search term (Dorian). If it does, it removes the search term (column1) from the result string, trims it, thus leaving the contents of column2.

Try it on the attached file (chrometest.txt - make sure to rename it to chrometest.htm)
EDIT : File upload was flagged as unsafe. Here's the html :

Code: Select all

<html><body>
<table>
  <tr>
    <th>First name</th>
    <th>Last name</th>
  </tr>
  <tr>
    <td>John</td>
    <td>Doe</td>
  </tr>
  <tr>
    <td>Jane</td>
    <td>Doe</td>
  </tr>
  <tr>
    <td>Dorian</td>
    <td>Ellis</td>
  </tr>

</table>
</body>
</html>


As ever, there may be other ways to do this, but this one works.

Code: Select all

let>TheURL=d:/chrometest.htm
Let>MySearchText=Dorian

Let>CHROMEDRIVER_EXE=c:\chromedriver.exe
ChromeStart>session_id
ChromeNavigate>session_id,url,%TheURL%
wait>4

//Extract all rows
ChromeFindElements>session_id,tag name,tr,elements

//Loop through all rows, extracting element data
Let>CheckLoop=0
Repeat>CheckLoop
  Let>CheckLoop=CheckLoop+1
  ChromeGetElementData>session_id,elements_%CheckLoop%,text,TheText

 //Does this row contain our search text?
  Pos>MySearchText,TheText,1,PosSearch,
  
  //If so, do something with it
  If>PosSearch>0
    StringReplace>TheText,MySearchText,,TheText
    LTrim>TheText,TheText
    MDL>TheText
  Endif
Until>CheckLoop,elements_count

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

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