Javascript iMacros nested while loop (two loops in macro)

1.2k views Asked by At

I have been trying to get a loop inside of a loop to work. By that I mean I'm going to multiple pages in a website, clicking all the links in a table and extraction information from the next page after following the link. I've found this question here http://www.stackoverflow.com/questions/18402012/nested-loop-in-imacros-2nd-loop, yet I can not get it to work for me. The problem is happening at the "while(true)" or "n = 1" section I think. My code looks this:

const L = "\n";

var macro;
macro = "CODE:";
macro += "SET !ERRORIGNORE YES" + L; 
macro += "SET !DATASOURCE DailyCitySummaries.csv" + L;
macro += "SET !DATASOURCE_LINE {{i}}" + L; 
macro += "SET !WAITPAGECOMPLETE YES" + L; 
macro += "SET !EXTRACT_TEST_POPUP NO" + L; 
macro += "URL GOTO=http://{{!COL1}}" + L;
macro += "FRAME F=0" + L;
macro += "TAG POS=1 TYPE=A ATTR=HREF:/page/Results.cfm?type=location=* EXTRACT=TXT" + L; 
macro += "SAVEAS TYPE=EXTRACT FOLDER=/root/Desktop/ FILE=CityName.txt" + L; 

var macro1;
macro1 = "CODE:";
macro1 += "TAG POS=1 TYPE=A ATTR=TXT:city<SP>{{n}}" + L;
macro1 += "TAG POS=1 TYPE=STRONG ATTR=TXT:city<SP>* EXTRACT=TXT" + L; 
macro1 += "TAG POS=1 TYPE=TABLE ATTR=TXT:* EXTRACT=TXT" + L; 
macro1 += "SAVEAS TYPE=EXTRACT FOLDER=/root/Desktop FILE=CityDetails.csv" + L;

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

     //set counter
    var n = 1   //this is only following the first link, I want all of them!
    while(true)   //this is suppose to be an infinite loop 
        {
        iimSet("n", n)
        var ret=iimPlay(macro1);
//kill loop when it comes to end and go to next location (first macro)
        if(ret<0) 
            {
            break;
            }
//increase counter
        n++;
        } //end of while loop
} //end of for loop

Is it my formating or something. What the code does now is go to the web page, extract a table, click a link in the overview table, go to a detail page for that link, then extract a table for the detail page. The problem is, it's not clicking all the links in the overview table, only the first one, and then it goes through the first loop for the next overview table. The number of links is different for every overview table. I know didely about JavaScript, so any pointers would be greatly appreciated.

1

There are 1 answers

0
Thomas Paine On

In case anyone wants to see the code in what ended up working:

const L = "\n";

var macro;
macro = "CODE:";
macro += "SET !ERRORIGNORE YES" + L; 
macro += "SET !DATASOURCE DailyCitySummaries.csv" + L;
macro += "SET !DATASOURCE_LINE {{i}}" + L; 
macro += "SET !WAITPAGECOMPLETE YES" + L; 
macro += "SET !EXTRACT_TEST_POPUP NO" + L; 
macro += "URL GOTO=http://{{!COL1}}" + L;
macro += "FRAME F=0" + L;
macro += "TAG POS=1 TYPE=A ATTR=HREF:/page/Results.cfm?type=location=* EXTRACT=TXT" + L; 
macro += "SAVEAS TYPE=EXTRACT FOLDER=/root/Desktop/ FILE=CityName.txt" + L; 

var macro1;
macro1 = "CODE:";
macro1 += "TAG POS=1 TYPE=A ATTR=TXT:city<SP>{{n}}" + L;
macro1 += "TAG POS=1 TYPE=STRONG ATTR=TXT:city<SP>* EXTRACT=TXT" + L; 
macro1 += "TAG POS=1 TYPE=TABLE ATTR=TXT:* EXTRACT=TXT" + L; 
macro1 += "SAVEAS TYPE=EXTRACT FOLDER=/root/Desktop FILE=CityDetails.csv" + L;
macro1 += "BACK" + L;   // The difference that made a difference

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

     //set counter
    var n = 1 
    while(true)   //this is an infinite loop 
        {
        iimSet("n", n)
        var ret=iimPlay(macro1);
//kill loop when it comes to end and go to next location (first macro)
        if(ret<0) 
            {
            break;
            }
//increase counter
        n++;
        } //end of while loop
} //end of for loop