Determine row in which a string is in an HTML table

Discussions and Tech Support related to website data extraction, screen scraping and data mining using iMacros.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information:CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
golyalpha
Posts: 3
Joined: Sat Jul 01, 2017 12:21 pm

Determine row in which a string is in an HTML table

Post by golyalpha » Sat Jul 01, 2017 12:28 pm

How can I determine in which row of a table a certain string is and save it into a variable using iMacros? (and use that position to later click a link with iMacros)

Thanks! :)
chivracq
Posts: 8525
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Determine row in which a string is in an HTML table

Post by chivracq » Sat Jul 01, 2017 2:44 pm

golyalpha wrote:How can I determine in which row of a table a certain string is and save it into a variable using iMacros? (and use that position to later click a link with iMacros)

Thanks! :)
CIM...! :mrgreen: (Read my Sig...)

Interesting Case, ah-ah...! :D

But, yep..., I have a "Method" to do that, based a bit on my "Method to determine Dynamic ID's" but it's a bit "cumbersome" and you probably won't understand it if I have to explain it "generically"...
=> Post your URL with some concrete Example of what you want exactly... (Or some public Site with a concrete Example if you don't want to post your "real" URL... But try to find a very similar Table with the same HTML Structure as iMacros heavily relies on the HTML Structure of Pages/Elements...)

Oh...!, 2 Methods actually...! I already implemented this very same Functionality about 4 or 5 years ago and it still works fine, in some Script that I still use daily, even if I realize now that I would come up now with a much simpler Implementation, ah-ah...! But as it still works perfectly, I never bothered to redo/improve this part of Code...
Oh yeah...!, just checked it, and that was the longest 'EVAL()' Statement I ever produced, ah-ah...!, 1.6Kb (= 1600 Chars in just one Statement...!), oops...! Yep, I could definitely make it muuuuuch shorter now, but I didn't really master JavaScript Array Functions at that time...
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
golyalpha
Posts: 3
Joined: Sat Jul 01, 2017 12:21 pm

Re: Determine row in which a string is in an HTML table

Post by golyalpha » Sun Jul 02, 2017 7:24 am

OS: Win10 x64 PRO
Browser: Firefox 54.0.1 x86
iMacros: Latest from Firefox Addons... (Not sure what version is there.)

Here's the link to a copied table from the page (the actual table is protected by login): http://imacros-table.9e.cz/

(there's some CSS on the actual page of course, and I added the "C" into link that the macro is supposed to click, just so you can see it, in the actual table, the "C" isn't there, there are some images placed by the CSS, and four (or five) different links in total)

Edit: You'll get a 404 error if you try to click that C. What's on the following page is not important though.
chivracq
Posts: 8525
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Determine row in which a string is in an HTML table

Post by chivracq » Sun Jul 02, 2017 8:15 am

golyalpha wrote:

Code: Select all

OS: Win10 x64 PRO
Browser: Firefox 54.0.1 x86
iMacros: Latest from Firefox Addons... (Not sure what version is there.)
Here's the link to a copied table from the page (the actual table is protected by login): http://imacros-table.9e.cz/

(there's some CSS on the actual page of course, and I added the "C" into link that the macro is supposed to click, just so you can see it, in the actual table, the "C" isn't there, there are some images placed by the CSS, and four (or five) different links in total)

Edit: You'll get a 404 error if you try to click that C. What's on the following page is not important though.
FCI: Will probably be iMacros for FF v9.0.3 then...
Check the Forum Rules btw if you want some Explanation on how to check your Version, ah-ah...! :roll:

Yep, no Pb for the Link...
Perfect-perfect for the Test Site with the more-or-less real Table I guess... That's perfect, exactly what I need to understand a bit and do some Testing...! :D
But hum, from the "concrete Example" I was asking, I miss the Info how do you know that "Target" is your "Target"...?
What is your "Input" info to take Decisions...?
(And hum..., I have the Feeling your Qt is about a Game, but I normally don't help Users (to cheat) for Games/Votes/Spam/Hacking/DDOS/SM/etc... But, OK, fire it, and I'll see...!)

:arrow: Consider checking "Relative Positioning", until I have all Info about what exactly you want to do, I have a "Feeling" that "RP" could be your easiest Solution, ah-ah...! :idea:
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
golyalpha
Posts: 3
Joined: Sat Jul 01, 2017 12:21 pm

Re: Determine row in which a string is in an HTML table

Post by golyalpha » Sun Jul 02, 2017 12:18 pm

That string "Target" will be replaced by something else. The problem is, that while the string will always be in that table, it can move around a lot (sometimes it can be on the second row, sometimes on the 10th...). User could either input the correct string to search for in the table through an input box that would pop up at the begining of a macro.
chivracq
Posts: 8525
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Determine row in which a string is in an HTML table

Post by chivracq » Sun Jul 02, 2017 5:48 pm

golyalpha wrote:That string "Target" will be replaced by something else. The problem is, that while the string will always be in that table, it can move around a lot (sometimes it can be on the second row, sometimes on the 10th...). User could either input the correct string to search for in the table through an input box that would pop up at the begining of a macro.
Well, simply use Relative Positioning like I suggested:

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
TAB T=1
URL GOTO=http://imacros-table.9e.cz/
TAG POS=1 TYPE=TD ATTR=TXT:Target
TAG POS=R1 TYPE=SPAN ATTR=TXT:C
Your Table only contains one Link but I reckon each Row will have its own Link...

Or this one works as well for your Table:

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
TAB T=1
URL GOTO=http://imacros-table.9e.cz/
TAG POS=1 TYPE=TD ATTR=TXT:Target
'TAG POS=R1 TYPE=SPAN ATTR=TXT:C EXTRACT=HTM
'TAG POS=R2 TYPE=A ATTR=TXT:* EXTRACT=HTM
TAG POS=R2 TYPE=A ATTR=TXT:*
(Tested on iMacros for FF v8.8.2, Pale Moon v26.3.3, Win10-x64.)
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
cbelerique
Posts: 4
Joined: Fri Aug 18, 2017 6:25 pm

Re: Determine row in which a string is in an HTML table

Post by cbelerique » Fri Aug 18, 2017 7:50 pm

I am having a similar issue. I've found a solution in JQuery that searches for a TD with a specified word and returns its row number but adding it to a macro (or JS in iMacros) has been too difficult for me to figure out. I'm interested to see where this goes and I'll post the JQuery below in case it helps someone.

Code: Select all

$("tr td:contains('WORDTOSEARCHFOR')").each(function(){
    var $tr = $(this).closest('tr');
    var myRow = $tr.index();
  alert(myRow) ;
});
Hope we get a solution for this!
chivracq
Posts: 8525
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Determine row in which a string is in an HTML table

Post by chivracq » Fri Aug 18, 2017 9:33 pm

cbelerique wrote:I am having a similar issue. I've found a solution in JQuery that searches for a TD with a specified word and returns its row number but adding it to a macro (or JS in iMacros) has been too difficult for me to figure out. I'm interested to see where this goes and I'll post the JQuery below in case it helps someone.

Code: Select all

$("tr td:contains('WORDTOSEARCHFOR')").each(function(){
    var $tr = $(this).closest('tr');
    var myRow = $tr.index();
  alert(myRow) ;
});
Hope we get a solution for this!
(F)CIM...! :mrgreen: (Read my Sig...)

I have a fairly simple Solution in pure '.iim' which is actually very similar to yours in JQuery, with a Double 'split()' (on your Search Keyword and on "tr" as well...!) + the 'length' Property of the Array returned by 'split()' (-1) which is similar to your 'index()'...

But what is your Purpose for the Row-Nb...? Like @OP, if you need it to reuse it for some further Action (Select/Tag/Input/Extract) in the same Table on the same Row, then is Relative Positioning even much simpler to use, like I demonstrated for @OP... (who didn't follow up anymore btw, grrr...!).
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
Post Reply