Ruud
Ruud

Reputation: 249

Nested loop in iMacros (2nd loop)

Hoping you could help me make a nested loop work with iMacros.

  1. First loop: Loops all rows of source.csv (col A, multiple rows)
  2. Second loop (X): Grab all the List elements found on hxxp://site.com/?what={{!COL1}}

After a lot of work I feel I am finally getting somewhere:

The problem in the script is that I cannot get Loop {{n}} to grab all the positions of the page (list items). Any help would be highly appreciated.

var macro;
macro =  "CODE:";
macro +=  "VERSION BUILD=000000" + "\n"; 
macro +=  "TAB T=1" + "\n"; 
macro +=  "SET !ERRORIGNORE YES" + "\n"; 
macro +=  "SET !EXTRACT_TEST_POPUP NO" + "\n"; 
macro +=  "SET !DATASOURCE source.csv" + "\n";
macro +=  "SET !DATASOURCE_COLUMNS 1 " + "\n";
macro +=  "SET !LOOP 1" + "\n"; 
//macro +=  "SET !TIMEOUT 3" + "\n";
macro +=  "SET !DATASOURCE_LINE {{i}}" + "\n"; 
macro +=  "URL GOTO=http://site.com/?what={{!COL1}} " + "\n"; 
macro +=  "TAG POS={{n}} TYPE=LI ATTR=CLASS:classofdiv" + "\n"; 
macro +=  "TAG POS={{n}} TYPE=SPAN ATTR=TXT:* EXTRACT=TXT" + "\n"; 
macro +=  "SAVEAS TYPE=EXTRACT FOLDER=* FILE=All_list_items.txt" + "\n"; 

for (var i=1;i<20;i++)
{
iimSet("i",i)
iimPlay(macro)
iimSet("n",i)//<-- How to grab all the TAG POS of the website?
}

Edit 1.1 - I should point out that I use the term {{n}} as example to loop list items (div-ul-li-span). If replaced with 'POS=1' the script works, but ofcourse only saves the first list item instead of all the list items.

Upvotes: 2

Views: 5525

Answers (1)

edinvnode
edinvnode

Reputation: 3547

So you want to grab all the positions of POS=x ?

Try this out:

var macro;
macro =  "CODE:";
macro +=  "VERSION BUILD=000000" + "\n"; 
macro +=  "TAB T=1" + "\n"; 
macro +=  "SET !ERRORIGNORE YES" + "\n"; 
macro +=  "SET !EXTRACT_TEST_POPUP NO" + "\n"; 
macro +=  "SET !DATASOURCE source.csv" + "\n";
macro +=  "SET !DATASOURCE_COLUMNS 1 " + "\n";
macro +=  "SET !LOOP 1" + "\n"; 
//macro +=  "SET !TIMEOUT 3" + "\n";
macro +=  "SET !DATASOURCE_LINE {{i}}" + "\n"; 
macro +=  "URL GOTO=http://site.com/?what={{!COL1}} " + "\n"; 

var macro1;
macro1 =  "CODE:";
macro1 +=  "TAG POS={{n}} TYPE=LI ATTR=CLASS:classofdiv" + "\n"; 
macro1 +=  "TAG POS={{n}} TYPE=SPAN ATTR=TXT:* EXTRACT=TXT" + "\n"; 
macro1 +=  "SAVEAS TYPE=EXTRACT FOLDER=* FILE=All_list_items.txt" + "\n"; 

for (var i=1;i<20;i++)
{
iimSet("i",i)
iimPlay(macro)


//set counter
var n=1;
//this is infinite loop
while(true)
{
iimSet("n",n)//<-- How to grab all the TAG POS of the website?

var ret=iimPlay(macro1);

//if macro didn't find any more positions it breaks from this loop
//and reads new link from the csv file
if(ret<0)
{
break;
}

//increase counter
n++;
}//end of while loop

}//end of for loop

This will enable macro1 to loop inside while loop as long as there is new position each time. This might not work so give us feedback.

Upvotes: 2

Related Questions