简体   繁体   中英

Javascript iMacros nested while loop (two loops in macro)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM