Trying to make 2 loops work together

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
bb4
Posts: 4
Joined: Thu Jun 20, 2019 6:55 pm

Trying to make 2 loops work together

Post by bb4 » Thu Jun 20, 2019 9:17 pm

Using:
FF ver. 56, Windows 7,32 bit, Imacros 8.9.7

Hello;
I am not good at Imacros,
I want to make two loop macros work together, but can not figure it out how,
I have looked through nested loop macros on this forum, but I could not figure it out to make these work.
I appreciate your help to make this work.

1 - 1st. macro, search a website with keyword from csv file, e.g. 20 keywords, the loop works when I put the number of keywords in the loop play button.

2- 2nd. macro
this macro extract data of result page,
the result data are in 10 rows, each row has 10 cell of data,
this marco extract all rows of data one by one and save it to a csv file, it works by itself.

but I want to make these two macros work together, when 1st. keyword search is done, 2nd macro starts to work
and extract data, when done, then let the 1st. macro continues the search with the 2nd. keyword, and when the result of 2nd. keyword search appear, the second macro start to extract the data, and so on, until the end of keywords in csv file.

here is the 1st. macro:

Code: Select all

VERSION BUILD=8970419 RECORDER=FX
SET !ERRORIGNORE YES
SET !EXTRACT_TEST_POPUP NO
SET !DATASOURCE keywords.csv
SET !DATASOURCE_COLUMNS 1
SET !LOOP 1
SET !DATASOURCE_LINE {{!LOOP}} 
TAB T=1

URL GOTO=https://WWW.WEBSITE.COM
TAG POS=1 TYPE=INPUT:TEXT ATTR=* CONTENT={{!COL1}}
TAG XPATH="(//button[contains(@class,'btn bbtn-anger')])[1]"
here is the 2nd. macro:

Code: Select all

'ROW 1 CELL 1
TAG XPATH="(/html/body/div[1]/div/div/div/div/div/div[7]/div/div[3]/table/tbody/tr[{{!LOOP}}]/td[2]/div/div/div[1])" EXTRACT=TXT

SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 2
TAG XPATH="(//td[@class='column-pvo'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 3
TAG XPATH="/html/body/div[1]/div/div/div/div/div/div[7]/div/div[3]/table/tbody/tr[{{!LOOP}}]/td[4]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 4
TAG XPATH="/html/body/div[1]/div/div/div/div/div/div[7]/div/div[3]/table/tbody/tr[{{!LOOP}}]/td[5]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 5
TAG XPATH="(//td[@class='column-va'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 6
TAG XPATH="(//td[@class='column-onp'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 7
TAG XPATH="(//td[@class='column-ofp'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 8
TAG XPATH="(//td[@class='column-sep])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 9
TAG XPATH="(//td[@class='column-ctop'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 10
TAG XPATH="(//td[@class='column-kwpo'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

SAVEAS TYPE=EXTRACT FOLDER=* FILE=_Extracted-Data.csv
SET !EXTRACT NULL
thank you for your help.
Last edited by bb4 on Fri Jun 21, 2019 6:55 am, edited 1 time in total.
chivracq
Posts: 8309
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: I want to make 2 loops work togher, help needed

Post by chivracq » Thu Jun 20, 2019 10:14 pm

bb4 wrote:
Thu Jun 20, 2019 9:17 pm
Using:

Code: Select all

FF ver. 56, Windows 7,32 bit, Imacros 8.9.7
Hello;
I am not good at Imacros,
I want to make two loop macros work together, but can not figure it out how,
I have looked through nested loop macros on this forum, but I could not figure it out to make these work.
I appreciate your help to make this work.

1 - 1st. macro, search a website with keyword from csv file, e.g. 20 keywords, the loop works when I put the number of keywords in the loop play button.

2- 2nd. macro
this macro extract data of result page,
the result data are in 10 rows, each row has 10 cell of data,
this marco extract all rows of data one by one and save it to a csv file, it works by itself.

but I want to make these two macros work together, when 1st. keyword search is done, 2nd macro starts to work
and extract data, when done, then let the 1st. macro continues the search with the 2nd. keyword, and when the result of 2nd. keyword search appear, the second macro start to extract the data, and so on, until the end of keywords in csv file.

here is the 1st. macro:

Code: Select all

VERSION BUILD=8970419 RECORDER=FX
SET !ERRORIGNORE YES
SET !EXTRACT_TEST_POPUP NO
SET !DATASOURCE keywords.csv
SET !DATASOURCE_COLUMNS 1
SET !LOOP 1
SET !DATASOURCE_LINE {{!LOOP}} 
TAB T=1

URL GOTO=https://WWW.WEBSITE.COM
TAG POS=1 TYPE=INPUT:TEXT ATTR=* CONTENT={{!COL1}}
TAG XPATH="(//button[contains(@class,'btn bbtn-anger')])[1]"
here is the 2nd. macro:

Code: Select all

'ROW 1 CELL 1
TAG XPATH="(/html/body/div[1]/div/div/div/div/div/div[7]/div/div[3]/table/tbody/tr[{{!LOOP}}]/td[2]/div/div/div[1])" EXTRACT=TXT

SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 2
TAG XPATH="(//td[@class='column-pvo'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 3
TAG XPATH="/html/body/div[1]/div/div/div/div/div/div[7]/div/div[3]/table/tbody/tr[{{!LOOP}}]/td[4]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 4
TAG XPATH="/html/body/div[1]/div/div/div/div/div/div[7]/div/div[3]/table/tbody/tr[{{!LOOP}}]/td[5]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 5
TAG XPATH="(//td[@class='column-va'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 6
TAG XPATH="(//td[@class='column-onp'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 7
TAG XPATH="(//td[@class='column-ofp'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 8
TAG XPATH="(//td[@class='column-sep])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 9
TAG XPATH="(//td[@class='column-ctop'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

'ROW 1 CELL 10
TAG XPATH="(//td[@class='column-kwpo'])[{{!LOOP}}]" EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}

SAVEAS TYPE=EXTRACT FOLDER=* FILE=_Extracted-Data.csv
SET !EXTRACT NULL
thank you for your help.
"help needed" is always a bit "useless" in a Thread Title, all Threads on the Forum are about "help needed"..., but if you could correct the Typo about "together"...?

Nothing specific to iMacros for FF, correct Sub-Forum would have been the 'General' one, unless you want to use a '.js' Script, which could indeed be a "Possibility", to handle the "Main" + the "Nested" Looping.
But your Scenario is a "classic" Case of a 'Nested Loop' + 'Modulo[10]' for the 2nd part. And it can fairly easily be done in pure '.iim', which would "keep" your Macro working if you ever want to update FF to some later Version (as you would then have to update iMacros also, to v10.0.2 for FF, which doesn't support '.js' Scripts anymore), or ever want to use a different Browser... (And you would then loop your Macro 200 times...) You'll find several similar Threads on the Forum if you search the Forum using "nested+loop+modulo"... :idea:

Concerning your 2nd Macro, the first 9x "SET !VAR1 {{!EXTRACT}}" have no Use, only the last one has a "Purpose"... The 'EXTRACT' Mechanism with the '!EXTRACT' Built-in Var is already automatically "cumulative"..., and keeps all previous Extracts... Those 9x Statements would only be "useful" if you kept each extracted Value in a different Var, but that's apparently not the case...
Your "SET !EXTRACT NULL" has also no Use as '!EXTRACT' already automatically gets reset after a 'SAVEAS TYPE=EXTRACT'.
And your Use of 'XPATH' for all 'TAG' + 'EXTRACT' is also a bit "surprising", you could probably have used "directly" the "Standard" 'TAG POS' Mode, with the Looping on the 'POS' Parameter... => Using the same Class you already do for 7x Cells, and probably 'Relative Positioning' for Cells [1+3+4] which would probably be more "reliable" than the long CSS Path you are currently using, which could easily get "broken" if anything changed concerning the Layout or even just the Content of the Page (Paragraph added or removed, an Ad being displayed, etc...). (My personal "Opinion"... :P )
- (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...
bb4
Posts: 4
Joined: Thu Jun 20, 2019 6:55 pm

Re: Trying to make 2 loops work together

Post by bb4 » Fri Jun 21, 2019 7:45 am

Thank you for your reply,
sorry about the typo,
I took off the unnecessary codes from my macro,
about xpath, well I could not make it recognize cells, so xpath was my last resort,

for now I intent to stay with this older ver. of Imacros, which works fine and I don't intent to upgrade my FF either, latest ver. of FF, over ver 56 is useless for me, it takes lot of my computer resources, so until I buy newer computer I intent to use this ver of FF, it is good that I can use js with this version of Imacros but I don't know about js.

yes it is easy to make it work, but for experienced one,
I have already looked at similar nested loops scenarios on this forum, and I could not figure it out how to use Eval with my codes, perhaps little different than other loop on this forum, so that is why I posted my macros to see if anyone help me to make it work,

I guess this was your maximum help on my main problem, if anyone can help me with more tips/ codes/ link to make this works, it is appreciated.
thanks
bb4
Posts: 4
Joined: Thu Jun 20, 2019 6:55 pm

Re: Trying to make 2 loops work together

Post by bb4 » Fri Jun 21, 2019 8:12 am

chivracq, if you don't mind provide a link to this exact Modulo 10, instruction, tutorial, documentation.

thanks
chivracq
Posts: 8309
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Trying to make 2 loops work together

Post by chivracq » Fri Jun 21, 2019 4:01 pm

bb4 wrote:
Fri Jun 21, 2019 7:45 am
Thank you for your reply,
sorry about the typo,
I took off the unnecessary codes from my macro,
about xpath, well I could not make it recognize cells, so xpath was my last resort,

for now I intent to stay with this older ver. of Imacros, which works fine and I don't intent to upgrade my FF either, latest ver. of FF, over ver 56 is useless for me, it takes lot of my computer resources, so until I buy newer computer I intent to use this ver of FF, it is good that I can use js with this version of Imacros but I don't know about js.

yes it is easy to make it work, but for experienced one,
I have already looked at similar nested loops scenarios on this forum, and I could not figure it out how to use Eval with my codes, perhaps little different than other loop on this forum, so that is why I posted my macros to see if anyone help me to make it work,

I guess this was your maximum help on my main problem, if anyone can help me with more tips/ codes/ link to make this works, it is appreciated.
thanks
Oh...!, perfect for the Thread Title, Thanks... :D

Yep, nothing "wrong" with v8.9.7 for FF, it's still the best and most stable Version to use, and I use it myself also... (on FF v55.0.3)
bb4 wrote:
Fri Jun 21, 2019 8:12 am
chivracq, if you don't mind provide a link to this exact Modulo 10, instruction, tutorial, documentation.

thanks
Yeah, well, not complicated to search the Forum using the Search Keywords I gave you:
search.php?keywords=nested+loop+modulo

You'll need to use 'EVAL()' + 'ceil()' on '!LOOP'/10 for the '!DATASOURCE_LINE' in order to "stay" on the same Keyword in your '.CSV' for every 10 Loops.

... And to use the Remainder of the 'Modulo[10]' for each Row for the Results. The Cell Position within a Row can be hard-coded with [1-10] or you can also use the same 'Modulo[10]' Remainder.

And I think you are making a "Mistake" with all your 'TAG XPATH' Statements concerning your current Use of '{{!LOOP}}', except for 'CELL_1' for which it "luckily" works for "1".
What I mean is that you are actually "catching" all Cells "diagonally" in the Table... => Your 'CELL_6' is currently 'CELL_6' from 'ROW_6' and not from 'ROW_1' like you "think"... Am I correct...?

And much easier than your current Implementation with 'XPATH' would be if you can post the 'Class' for your 'CELL_1'...? Or for 'CELL_0'...?, there seems to be some "Header Col" in your Table for each Row...
=> Then it would be much easier to tag all next 9x Cells from a same Row with stg like:

Code: Select all

CELL_1:
TAG POS={{Var_Modulo[10]_on_LOOP}} TYPE=TD ATTR=CLASS:"Col1 Class" EXTRACT=TXT
CELL_2:
TAG POS=R1 TYPE=TD ATTR=* EXTRACT=TXT
CELL_3:
TAG POS=R1 TYPE=TD ATTR=* EXTRACT=TXT
'[...]
CELL_10:
TAG POS=R1 TYPE=TD ATTR=* EXTRACT=TXT

SAVEAS TYPE=EXTRACT FOLDER=* FILE=_Extracted-Data.csv
- (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