0

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.

2
  • I think that the problem is not in the nested while loop but in the ‘macro1’. Suggestion: (1st) take a look at the failure return code which you’re getting after var ret=iimPlay(macro1); . (2nd) Go manually to the page you need, play ‘macro1’ separately (better as iim-macro, not as js-script & also without / with a loop) and pay attention to the appearing error. Commented Jun 19, 2015 at 8:08
  • Thank you for your advise. I ended up doing as you suggested to trouble shoot. What was happening was in the second loop (macro1) I didn't have a "back" declaration at the end of the macro and the code wasn't finding the next link because it was on the prior page. It was a noob mistake I should have caught. Thank you again for your suggestion as it did result in me finding my error. Commented Jun 19, 2015 at 11:56

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.