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.

Moderators: Community Moderators, iMacros Moderators

Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the Google search box (at the top of each forum page) to see if a similar problem or question has already been addressed. This will search the entire contents of the forums as well as the iMacros Wiki.
3. We can respond much faster to your posts if you include the following information:

CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST

Answering your own posts (e.g. attempting to "bump" your topic) drops your topic from the list of unanswered threads, so it may actually receive less views.

Determine row in which a string is in an HTML table

by golyalpha on Sat Jul 01, 2017 5:28 am

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! :)
golyalpha
 
Posts: 3
Joined: Sat Jul 01, 2017 5:21 am

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

by chivracq on Sat Jul 01, 2017 7:44 am

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...
chivracq
 
Posts: 6490
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

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

by golyalpha on Sun Jul 02, 2017 12: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.
golyalpha
 
Posts: 3
Joined: Sat Jul 01, 2017 5:21 am

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

by chivracq on Sun Jul 02, 2017 1: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...
chivracq
 
Posts: 6490
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

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

by golyalpha on Sun Jul 02, 2017 5:18 am

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.
golyalpha
 
Posts: 3
Joined: Sat Jul 01, 2017 5:21 am

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

by chivracq on Sun Jul 02, 2017 10:48 am

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...
chivracq
 
Posts: 6490
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

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

by cbelerique on Fri Aug 18, 2017 12: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!
cbelerique
 
Posts: 4
Joined: Fri Aug 18, 2017 11:25 am

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

by chivracq on Fri Aug 18, 2017 2: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...
chivracq
 
Posts: 6490
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)


Return to Data Extraction and Web Screen Scraping

Who is online

Users browsing this forum: No registered users and 2 guests

-->