{ iMacros } How to Loop and Click Next? - imacros

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

This code will help you,
just an explanation to familiarize you with imacros.
You need to create a script.js file in Imacros and paste this code.
do while loop will run the code inside the Braces forever.
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.
The function iimGetLastExtract() will get the last extracted value from the code.
If the extracted text is next then break the infinite loop.
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);

Related

Imacros: Check if text exists

I need help with iMacros.
I have a task which consists of two parts:
1) Go to a website and fill out a form.
URL GOTO=https://example.com/registration
TAG POS=1 TYPE=INPUT:TEXT ATTR=ID:NQKtn CONTENT=ggg
2) Check if the text Mytext exists (which has to appear after filling out the form).
The task of the script is to save the string ggg to file.txt if the text is found, and to pass it if it's not.
How can I solve this problem? Thanks very much!
Example with Javascript and iMacros for Firefox:
var FilePath = "c:\\yourfile.txt";
var your_newtext = "ggg";
var macro = "CODE:";
macro += "URL GOTO=https://example.com/registration\n";
macro += "SET !ERRORIGNORE YES\n";
macro += "TAG POS=1 TYPE=INPUT:TEXT ATTR=ID:NQKtn CONTENT="+your_newtext+"\n";
macro += "WAIT SECONDS = 0.1\n";
macro += "TAG POS=1 TYPE=INPUT:TEXT ATTR=ID:NQKtn EXTRACT=TXT\n";
iimPlay(macro);
var last_extract = iimGetLastExtract();
var msg;
if(last_extract == "#EANF#" || last_extract == ""){
msg = "NOT FOUND\n";
}else{
msg = "FOUND: "+last_extract+"\n";
}
var file_o = imns.FIO.openNode(FilePath);
imns.FIO.appendTextFile(file_o, msg);

How to find 2 pictures via iMacros

For example:
On the page are 3 diferent picture
http://example.com/1.png
http://example.com/4.png
http://example.com/9.png
http://example.com/1.png
Please correct my code
TAG POS=1 TYPE=DIV ATTR=ID:site_loader EXTRACT=HTM
SET !VAR1 EVAL("var s=\"{{!EXTRACT}}\"; for(var i = 0; i < 9; i++) {var res=(s.split(i.\".png\").length - 1); MacroError(i); if (res=2) break; }")
TAG POS=1 TYPE=IMG ATTR=SRC:/surf/{{res}}.png
What else??
How to find 2 same picture and Save or click it ?
For your case I suggest this way:
SET listNum EVAL("\"{{!EXTRACT}}\".match(/\d(?=\.png)/g).toString();")
SET doubleNum EVAL("var d; var a = \"{{listNum}}\".split(\",\"); for (i in a) {if (a.indexOf(a[i]) != i) {d = a[i]; break;}} d;")
TAG POS=1 TYPE=IMG ATTR=SRC:/surf/{{doubleNum}}.png

Waiting 20 seconds in iMacros

U have imacros script and I want to add wait seconds 60 When i reach to 20.
This is my code now:
var macro;
macro = "CODE:";
macro += "URL GOTO=http://example.com/msg?uid={{i}}\n";
macro +="TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:msg ATTR=ID:subject CONTENT=hello\n";
macro +="TAG POS=1 TYPE=TEXTAREA FORM=NAME:msg ATTR=ID:message CONTENT=hi\n";
macro +="TAG POS=1 TYPE=INPUT:IMAGE FORM=NAME:msg ATTR=ID:btn_save";
for (var i=1;i<300;i++){
iimSet("i",i)
iimPlay(macro)
}
iimDisplay("Script completed.");
And I also tried this one:
var macro;
macro = "CODE:";
macro += "URL GOTO=http://example.com/msg?uid={{i}}\n";
macro +="TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:msg ATTR=ID:subject CONTENT=hello\n";
macro +="TAG POS=1 TYPE=TEXTAREA FORM=NAME:msg ATTR=ID:message CONTENT=hi\n";
macro +="TAG POS=1 TYPE=INPUT:IMAGE FORM=NAME:msg ATTR=ID:btn_save";
iimDisplay("Send Macro via iimPlay");
retcode = iimPlay(macro);
var i=2
while(true){
if(i%20==0){
iimPlay("CODE: WAIT SECONDS=60")
}
iimSet("i",i);
iimPlay(macro);
}
for (var i=1;i<999;i++){
iimSet("i",i)
iimPlay(macro)
}
iimDisplay("Script completed.");
But both code are not working for me. Can anybody help me? Thank you !
In your first code change the loop in the following way:
for (i = 1; i < 300; i++){
if (i == 20)
iimPlayCode("WAIT SECONDS=60");
iimSet("i", i);
iimPlay(macro);
}

Imacros - Invitation code missing 3 letters/numbers

IMACROS.
I have to find out a invitation code to a site, it's a code that contains letters(a-z) and numbers(0-9). There are 3 missing, the XXX for exemple. But, how do I make a "for" to considerate letters and numbers on Imacros? I have to use javascript?
This is the code I have:
VERSION BUILD=8820413 RECORDER=FX
TAB T=1
TAB CLOSEALLOTHERS
URL GOTO=(url site)
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:confirmcod.php ATTR=NAME:cod CONTENT=123XXXabc
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ACTION:confirmcod.php ATTR=*
One way to do this without using Javascript is to preload a datasource with all of the possible combinations for the link. Iterate over this document until you find the proper link.
VERSION BUILD=8820413 RECORDER=FX
TAB T=1
TAB CLOSEALLOTHERS
SET !DATASOURCE c:\mysource
SET !DATASOURCE_COLUMNS 1
SET !DATASOURCE_LINE {{!LOOP}}
URL GOTO=(url site)
TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:confirmcod.php ATTR=NAME:cod CONTENT={{!COL1}}
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ACTION:confirmcod.php ATTR=*
Wiki page for iMacro Datasource.
If you want to use javascript you will need to use iimPlay to generate the macro code on the fly. The example below iterates checks a random strings until iMacros returns 1 or sOK.
// possible keys for the link
var keys = '0123456789abcdefghijklmnopqrstuvwxyz';
// array to store past keys
var keyArray = [];
var myKey = "";
var i, j, k = 0;
var pageNotFound = true;
var macro = "";
var retCode = 0;
var myURL = "http://www.google.com";
do
{
i = keys.charAt(Math.floor(Math.random()*keys.length))
j = keys.charAt(Math.floor(Math.random()*keys.length));
k = keys.charAt(Math.floor(Math.random()*keys.length));
myKey = i + j + k;
if (keyArray.indexOf(myKey, 0) < 0)
{
keyArray.push(myKey);
// run imacro code with this key
macro = "CODE:";
macro += "TAB T=1\n";
macro += "TAB CLOSEALLOTHERS\n";
macro += "URL GOTO=" + myURL + "\n";
macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=ID:gbqf ATTR=ID:gbqfq CONTENT=" + myKey + "\n";
macro += "TAG POS=1 TYPE=BUTTON FORM=ID:gbqf ATTR=ID:gbqfb\n";
retCode = iimPlay(macro);
// check if the page is found
if (retCode)
{
// if page is found set pageNotFound = false;
pageNotFound = false;
}
} else {
// key has been used already, try a different one.
}
} while(pageNotFound);

Update link in item?

I have the following small script
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
using (new Sitecore.SecurityModel.SecurityDisabler())
{
foreach (Sitecore.Data.Items.Item child in item.Children)
{
foreach (ItemLink link in child.Links.GetAllLinks())
{
Item itm = link.GetTargetItem();
if (itm != null) {
Response.Write(link.TargetPath + " (" + itm.Paths.IsMediaItem + ", " + itm.ID + ")" + "<br/>");
} else
{
Response.Write("<span style='color:red;font-weight:bold;'>NULL ITEM ("+ link.TargetPath + ")</span><br/>");
}
}
if (item.Paths.ContentPath.Split("/".ToCharArray()).Length <= 10)
RecurseLinks(child, reset);
}
}
This loops through all items (and children) from a specified startpath and gets all links defined in the items.
Some of the links i need to update, as some of them are currently defined with a absolute path, and not the ID of the item that it is linked to (media or content item).
How would i achive this in the mentioned script?
The "rebuild link database" action should work.
The empty ID's are probably caused by broken links (target items not found in the database at the time of updating the links).
So rebuilding the links via the control panel, or in your code (see LinkDatabaseHelper) should work.