Two loops in one imacros

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
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
Chilly_Bang
Posts: 29
Joined: Tue Jan 27, 2015 9:13 am

Two loops in one imacros

Post by Chilly_Bang » Sat May 04, 2019 3:30 pm

i need to loop through urls, like

Code: Select all

URL GOTO=https://example.com/page/{{!LOOP}}
which is always working. An then i want to loop through all links on every looped url and save them, like with

Code: Select all

TAG POS={{!LOOP}} TYPE=A ATTR=CLASS:list EXTRACT=HREF
which isn't working - this extracts only the first occurence.

However, if i add something like

Code: Select all

TAG POS=1 TYPE=A ATTR=CLASS:list EXTRACT=HREF
TAG POS=2 TYPE=A ATTR=CLASS:list EXTRACT=HREF
TAG POS=3 TYPE=A ATTR=CLASS:list EXTRACT=HREF
each link is extracted like i expect it from the loop.

How can i use the loop on extracting links?

Update

With this code i've mastered to loop through links on the page:

Code: Select all

var macro;
macro =  "CODE:";
macro +=  "TAG POS={{i}} TYPE=A ATTR=CLASS:list EXTRACT=HREF" + "\n"; 
macro +=  "WAIT SECONDS=0.00001" + "\n"; 
for(i=1;i<=1000;i++){
iimSet("i",i);
iimPlay(macro);
}
But i'm fail on getting the imacro and JS code together... grrr
FCI: Win 7 x64 + Win10 x64 + FF 45.9.0 + iMacro for FF 9.0.3
chivracq
Posts: 8319
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Two loops in one imacros

Post by chivracq » Sat May 04, 2019 4:37 pm

Chilly_Bang wrote:
Sat May 04, 2019 3:30 pm
i need to loop through urls, like

Code: Select all

URL GOTO=https://example.com/page/{{!LOOP}}
which is always working. An then i want to loop through all links on every looped url and save them, like with

Code: Select all

TAG POS={{!LOOP}} TYPE=A ATTR=CLASS:list EXTRACT=HREF
which isn't working - this extracts only the first occurence.

However, if i add something like

Code: Select all

TAG POS=1 TYPE=A ATTR=CLASS:list EXTRACT=HREF
TAG POS=2 TYPE=A ATTR=CLASS:list EXTRACT=HREF
TAG POS=3 TYPE=A ATTR=CLASS:list EXTRACT=HREF
each link is extracted like i expect it from the loop.

How can i use the loop on extracting links?

Update

With this code i've mastered to loop through links on the page:

Code: Select all

var macro;
macro =  "CODE:";
macro +=  "TAG POS={{i}} TYPE=A ATTR=CLASS:list EXTRACT=HREF" + "\n"; 
macro +=  "WAIT SECONDS=0.00001" + "\n"; 
for(i=1;i<=1000;i++){
iimSet("i",i);
iimPlay(macro);
}
But i'm fail on getting the imacro and JS code together... grrr
FCI in your Sig is not the "best place" as you only have 1 Sig for the whole Forum and it gets "confusing" for older/future Threads... And you still need to hard-code it once in your OP when you open a new Thread...

Code: Select all

FCI: Win 7 x64 + Win10 x64 + FF 45.9.0 + iMacro for FF 9.0.3
And hum, I wanted to "bash" you for opening your Threads in the "correct" Sub-Forum, but OK, if using a '.js' Script now, from your Update, then the 'iMacros for FF' Sub-Forum is indeed the correct one now...

>>>

And using a '.js' Script will indeed be the best Solution..., as I guess the Nb of Links on each Page will probably be variable, then you cannot use 'Nested Loops' with a fixed Modulo... There are a few Solutions in pure '.iim', all a bit cumbersome, explained a bit in the following Thread which had a very similar Scenario like yours:
- Re: Conditional EVAL going to certain urls
Another one, and I don't remember if it was mentioned in that Thread, would be, if for example you know that you will never get more than 7 Links on one Page, to hard-code 7 or 8 or even 10 times the same Block of Code to handle the possible Extractions if the Links exist... And if on some Page you only have 5 Links, the Blocks [6-10] won't do anything... But that Method can be a bit "slow" if you have any 'URL GOTO' in the Block, or you need to add some extra Conditional Logic...

But OK, so a '.js' Script is indeed the best Solution, but you still need to implement the 'Nested Loops' Mechanism... (Many Threads already on the Forum if you search a bit...)
You already have the Inner Loop, now you miss the Outer Loop, that you'll have to base on 'iimGetExtract()' returning "#EANF#" for the Inner Loop to increment the Loop for the Outer Loop. (You cannot use the Return Code of the Inner Macro as 'EXTRACT' never fails (even if the HTML Element is not present)...)

And depending on what "other" Things you do in your Macro for each Link, you can build the Check either for the "current" Loop and current Link that won't exist, or you can already include it for the previous Link by already checking if the "next" Link will exist to then exit the Inner Loop... (=> So when handling the 'POS=3' Link, you already return the 'EXTRACT' on the 'POS=4' Link.)
- (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...
Chilly_Bang
Posts: 29
Joined: Tue Jan 27, 2015 9:13 am

Re: Two loops in one imacros

Post by Chilly_Bang » Sat May 04, 2019 5:27 pm

as I guess the Nb of Links on each Page will probably be variable
Correct.
to hard-code 7 or 8 or even 10 times ... But that Method can be a bit "slow"
Absolutely my thought. There could be more then 100 links to extract from one url, so hardcode is not a good idea...
FCI: Win 7 x64 + Win10 x64 + FF 45.9.0 + iMacro for FF 9.0.3
chivracq
Posts: 8319
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Two loops in one imacros

Post by chivracq » Sat May 04, 2019 5:56 pm

Chilly_Bang wrote:
Sat May 04, 2019 5:27 pm
as I guess the Nb of Links on each Page will probably be variable
Correct.
to hard-code 7 or 8 or even 10 times ... But that Method can be a bit "slow"
Absolutely my thought. There could be more then 100 links to extract from one url, so hardcode is not a good idea...
Alright, then you are already on the "right" path, you only need to add another 'for' Loop, or a 'while' or 'until' "Outer" Loop around your existing 'for' "Inner" Loop... :wink:
- (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...
Chilly_Bang
Posts: 29
Joined: Tue Jan 27, 2015 9:13 am

Re: Two loops in one imacros

Post by Chilly_Bang » Mon May 06, 2019 11:33 am

you only need to add another 'for' Loop, or a 'while' or 'until' "Outer" Loop around your existing 'for' "Inner" Loop
I'm afraid, what looks as clarity for you, is for me pretty foggy... Germans say "i'm on the end with my Latin":) I tested some kinds of notation - they all brought me not a little step further. The most promising was this one, but it doesn't work too:(

Code: Select all

var macro;
macro =  "CODE:";
macro +=  "TAB T=1";
macro +=  "SET !ERRORIGNORE YES";
macro +=  "URL GOTO=https://example.com/page/{{!LOOP}}";
macro +=  "SET !EXTRACT_TEST_POPUP NO";
macro +=  "WAIT SECONDS=2";
macro +=  "TAG POS={{i}} TYPE=A ATTR=CLASS:list EXTRACT=HREF" + "\n"; 
macro +=  "WAIT SECONDS=0.00001" + "\n"; 
for(i=1;i<=1000;i++){
iimSet("i",i);
iimPlay(macro);
}
macro +=  "SAVEAS TYPE=EXTRACT FOLDER=* FILE=links.csv";
FCI: Win 7 x64 + Win10 x64 + FF 45.9.0 + iMacro for FF 9.0.3
chivracq
Posts: 8319
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Two loops in one imacros

Post by chivracq » Mon May 06, 2019 12:20 pm

Chilly_Bang wrote:
Mon May 06, 2019 11:33 am
you only need to add another 'for' Loop, or a 'while' or 'until' "Outer" Loop around your existing 'for' "Inner" Loop
I'm afraid, what looks as clarity for you, is for me pretty foggy... Germans say "i'm on the end with my Latin":) I tested some kinds of notation - they all brought me not a little step further. The most promising was this one, but it doesn't work too:(

Code: Select all

var macro;
macro =  "CODE:";
macro +=  "TAB T=1";
macro +=  "SET !ERRORIGNORE YES";
macro +=  "URL GOTO=https://example.com/page/{{!LOOP}}";
macro +=  "SET !EXTRACT_TEST_POPUP NO";
macro +=  "WAIT SECONDS=2";
macro +=  "TAG POS={{i}} TYPE=A ATTR=CLASS:list EXTRACT=HREF" + "\n"; 
macro +=  "WAIT SECONDS=0.00001" + "\n"; 
for(i=1;i<=1000;i++){
iimSet("i",i);
iimPlay(macro);
}
macro +=  "SAVEAS TYPE=EXTRACT FOLDER=* FILE=links.csv";
Yeah, but you need 2 Loops, while your Script still only uses 1 Loop...! :roll:

The Concept of "Nested Loops" implies 2 (or more) Loops, nested inside each other, with in your specific Case, the Loop on the URL's being the "Outer" Loop, and the Loop on the Links within one same Page being the "Nested" or "Inner" Loop.

A simple Search on the Forum about "nested+loop[s]" will return quite a few Hits, with this Thread/Post for example which I think is probably the best one, where Advanced User @iimfun had posted a nice Example with 2x 'for' Loops and the 'break' Mechanism to "escape" from the Inner Loop:
- Re: Nested Loops with Javascript

The 'break' Mechanism in this Example is based on the Return Code for the Inner Macro, which I already mentioned earlier (in my 1st Reply), won't be applicable for you and where you'll have to use 'iimGetExtract()' to base the 'break' on the Condition if a "next" Link can still be extracted or not. But this is just one way to implement it, one of the easiest I would think, but there are probably more than 10 possible different Implementations...

>>>

+ For those interested, parallel Thread on SOF:
- Two different loops in one imacros
(... Only contains some kind of a bit "fake"/"troll" Answer about switching from iMacros to 'Kantu'...)
- (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