Arturino
Arturino

Reputation: 358

{ iMacros } How to Loop and Click Next?

first time using iMacros.

I want to run a loop 50x then when it is done, click the "Next" button and run the same loop again 50x, click "Next"... until "Next" is no longer clickable.

so far i have this working just 1 time:

SET !LOOP 1

TAG POS={{!LOOP}} TYPE=TD ATTR=CLASS:domain EXTRACT=TXT
SAVEAS TYPE=EXTRACT FOLDER=* FILE=Extract_{{!NOW:ddmmyy}}.csv

'click NEXT'
TAG POS=1 TYPE=A ATTR=TXT:Next

WAIT SECONDS=3

Upvotes: 2

Views: 4721

Answers (1)

Naren Murali
Naren Murali

Reputation: 56054

This code will help you,

just an explanation to familiarize you with imacros.

  1. You need to create a script.js file in Imacros and paste this code.
  2. do while loop will run the code inside the Braces forever.
  3. Then we hardcode the two macros being used as shown below and assign it a javascript variable, inside the loop iimplay() is a javascript function that can run imacros code. so we run (macroStart)"check next button" to check if next still exists.
  4. The function iimGetLastExtract() will get the last extracted value from the code.
  5. If the extracted text is next then break the infinite loop.
  6. else run the "extract and send to csv file" macro (macro2).

Code:

var macro1;
macro1 =  "CODE:";
macro1 +=  "SET !ERRORIGNORE YES" + "\n"; 
macro1 +=  "TAG POS=1 TYPE=A ATTR=TXT:Next EXTRACT=TXT" + "\n"; 

var macro2;
macro2 = "CODE:" + "\n"; 
macro2 +=  "SET !ERRORIGNORE YES" + "\n"; 
macro2 += "TAG XPATH=(/html/body//td[contains(@class,'domain')])[{{j}}] EXTRACT=TXT" + "\n"; 
macro2 += "SAVEAS TYPE=EXTRACT FOLDER=C:/Users/Naren/Desktop/ FILE=output.csv" + "\n"; 


var macro3;
macro3 = "CODE:" + "\n"; 
macro3 +=  "SET !ERRORIGNORE YES" + "\n"; 
macro3 += "TAG POS=1 TYPE=A ATTR=TXT:Next" + "\n"; 
macro3 += "WAIT SECONDS=5" + "\n"; 

for(var j = 1; j <= 50; j++){
    iimSet("j",j);
    iimPlay(macro2);
}
iimPlay(macro3);
do{
    iimPlay(macro1);
    var macro1Extract = iimGetLastExtract();

    if (macro1Extract !== 'Next') {
         break;
    }
    for(var j = 1; j <= 50; j++){
        iimSet("j",j);
        iimPlay(macro2);
    }
    iimPlay(macro3);
}while (true);

Upvotes: 4

Related Questions