Search code examples
javascriptfor-loopimacros

imacros/ javascript loop with condition on next button


noob on javascript, I tried to make a script that I found from StackoverFlow posts to work in my macros, but it does not work the way it should,

what I want to do is:

1- search google with keywords pull from from csv file, 2- extract and save the content. 3- go to next page of results, and extract and save, 4- it should continues the next button until there is no Next button, 5- then it should go to the next keyword from csv file.

the loop works, but I don't know why it only click on next button 4 times, and it continues on the next keyword.

I have tried 2 variations with "while" condition in the beginning but the result is same.

appreciate your help on this, I use FF 55, on windows 7 32 bit, and imacros 8.9.7 here is my macro, extracting and saving in not included,

                            var macro;
                            macro = "CODE:";
                            macro +=  "SET !ERRORIGNORE YES" + "\n";
                            macro +=  "SET !EXTRACT_TEST_POPUP NO" + "\n";
                            macro +=  "SET !DATASOURCE keywords.csv" + "\n";
                            macro +=  "SET !DATASOURCE_COLUMNS 1" + "\n";
                            macro +=  "SET !LOOP 1" + "\n";
                            macro +=  "SET !DATASOURCE_LINE {{RowNo}}" + "\n";

                            macro +=  "URL GOTO=https://www.google.com/ncr" + "\n";
                            macro +=  "TAG POS=1 TYPE=INPUT:TEXT FORM=ID:tsf ATTR=ID:lst-ib CONTENT={{!COL1}}" + "\n";
                            macro +=  "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:tsf ATTR=NAME:btnK" + "\n";

                            var NextBtnCheck=iimGetLastExtract();

                            var NextButton;
                            NextButton = "CODE:";
                            NextButton +=  "SET !ERRORIGNORE YES" + "\n";
                            NextButton +=  "SET !EXTRACT_TEST_POPUP NO" + "\n";
                            NextButton +=  "TAB T=1" + "\n";

                            NextButton +=  "WAIT SECONDS=1" + "\n";
                            NextButton +=  "TAG POS=1 TYPE=A ATTR=TXT:Next" + "\n";
                            NextButton +=  "WAIT SECONDS=0.5" + "\n";


                            for(var m=1; m<60; m++)
                            {iimSet("RowNo",m)
                            iimPlay(macro)

                            var n = 0
                            var n= iimPlay(NextButton);

                                do
                                    {
                                    iimSet("n",n)
                                    iimPlay(NextButton);

                                    if (NextBtnCheck !== "#EANF#");
                                    break;
                                    n++; 
                                    } 
                                    while(true)
                                    {
                                   iimSet("n",n);
                                    iimPlay(NextButton);
                                    }
                            }

Solution

  • I've made correction of your code like so:

    var macro =  "SET !DATASOURCE keywords.csv" + "\n";
    macro +=  "SET !DATASOURCE_LINE {{RowNo}}" + "\n";
    macro +=  "URL GOTO=https://www.google.com/ncr" + "\n";
    macro +=  "TAG POS=1 TYPE=INPUT:TEXT FORM=ID:tsf ATTR=ID:lst-ib CONTENT={{!COL1}}" + "\n";
    macro +=  "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:tsf ATTR=NAME:btnK" + "\n";
    
    var NextButton =  "TAB T=1" + "\n";
    NextButton +=  "WAIT SECONDS=1" + "\n";
    NextButton +=  "TAG POS=1 TYPE=A ATTR=TXT:Next" + "\n";
    NextButton +=  "WAIT SECONDS=0.5" + "\n";
    
    for(var m=1; m<60; m++) {
        iimSet("RowNo",m)
        iimPlayCode(macro)
    
        do {
            var NextBtnCheck = iimPlayCode(NextButton);
            if (NextBtnCheck != 1)
                break;
        } while(true)
    }