iMacros if/else statement how to? - imacros

I'm attempting to pull some data from a webpage. I'm running into trouble with an extra line of data that occurs in some situations.
Here is a block of my code:
VERSION BUILD=10.4.28.1074
TAB T=1
TAG POS=114 TYPE=TR ATTR=* EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}
SET !EXTRACT NULL
TAG POS=115 TYPE=TR ATTR=* EXTRACT=TXT
SET !VAR2 {{!EXTRACT}}
SET !EXTRACT NULL
TAG POS=116 TYPE=TR ATTR=* EXTRACT=TXT
ADD !EXTRACT {{!VAR1}}
ADD !EXTRACT {{!VAR2}}
SAVEAS TYPE=EXTRACT FOLDER=\\admin\Documents\iMacros FILE=extracttest2.csv
You'll notice this code has tag pos 114,115, and 116. However, sometimes I will only need to extract 114 and 115.
POS 114 starts with either "Owner:" or "Owners:". If "Owner:" then I only need to extract 114 and 115. If "Owners:" I need to extract 114, 115, and 116.
Is there a way to make an if/else statement or other type of rule that says if 114 contains the word "Owner" only POS 114 and 115 will be extracted. And if 114 contains the word "Owners" POS 114, 115 and 116 will be extracted?
Thanks in advance for any advice.

Here is one of the variants without applying the Scripting Interface. Instead of the line TAG POS=116 TYPE=TR ATTR=* EXTRACT=TXT add this block:
SET pos116 EVAL("('{{!VAR1}}'.match(/Owners:/)) ? 116 : 99999;")
SET !ERRORIGNORE YES
SET !TIMEOUT_STEP 0
TAG POS={{pos116}} TYPE=TR ATTR=* EXTRACT=TXT
SET !ERRORIGNORE NO
SET !TIMEOUT_STEP 6
SET !EXTRACT EVAL("('{{!EXTRACT}}' == '#EANF#') ? '' : '{{!EXTRACT}}';")
Note: I suppose that on the webpage there’s no element with TAG POS=99999 TYPE=TR ATTR=* .

Try something like this using the EVAL command so you can use if/else conditions:
SET NEWVAR EVAL("var s=\"{{!VAR1}}\"; if(!s.match(/owners/g)) {s=\"!VAR1\";} else {s=\"!VAR2\";")
Read more at the iMacros wiki

Related

iMacros: how to extract once every few loops

my script extract once every 60 loops, 1,60,120,180 .. etc
TAG POS=1, so I use made this with EVAL.
but my problem is that {{Datastart}} is empty when no value to extract
My question is how to make {{Datastart}} EVAL to work like {{1datastart}} ?
I mean to remain the first extract value from loop1 during the next 59 loop and to extract the value again at loop 61.
SET !ERRORIGNORE YES
SET 1datastart EVAL("var n='{{!LOOP}}'; var z; if((n%60)==1){z=\"1\";} else{z=0;}; z;")
TAG POS={{1datastart}} TYPE=DIV ATTR=CLASS:"*cell-body tablecell-date sortable column-sorted*"&&TXT:*,* EXTRACT=TXT
SET datastart EVAL("'{{!EXTRACT}}'.replace(/(Scheduled|Published)/g, '').trim();")
Prompt {{datastart}}
I am using (FCI): iMacros for CR v10.1.1 'PE', CR v105.0.5195.102 (_x64), Win10_x64. ('CR' = 'Chrome' / 'PE' = 'Personal Edition')

Wait for window content change or appear in Imacros

1 VERSION BUILD=844 RECORDER=CR
2 SET !ERRORIGNORE YES
3 SET !TIMEOUT_PAGE 35
4 TAB T=1
6 TAG POS=1 TYPE=A ATTR=TXT:Click<SP>here<SP>to<SP>earn
7 Wait Seconds=35
this is my code for some website i maked a fixed time but it actually it's a random time so how can i make imacros check this 'You can go back' text appear then continue the script without the 'WAIT' time
Thx in advance
You can just do the next step you want (like search for the "next" button) but add a SET !TIMEOUT_STEP 1000 (Any high number of seconds) before it and so instead of trying for 6 seconds (the default) and failing to find it iMacro will retry the action until the condition (STEP) matches or if the time runs out it will act as if the emelent was not found.
VERSION BUILD=844 RECORDER=CR
SET !ERRORIGNORE YES
SET !TIMEOUT_PAGE 35
TAB T=1
TAG POS=1 TYPE=A ATTR=TXT:Click<SP>here<SP>to<SP>earn
SET !TIMEOUT_STEP 600 ' or any other # of sec you think is ok
TAG POS=1 TYPE=A ATTR=TXT:You<SP>Can<SP>Go<SP>Back 'or other commands like search source
SET !TIMEOUT_STEP 6 ' remember to set back to 6 to not get stuck on other commands
http://wiki.imacros.net/!TIMEOUT_STEP
You can use
var macro,retcode;
macro = "CODE:";
macro += "SET !ERRORIGNORE NO" + "\n";
macro += "TAG POS=1 TYPE=A ATTR=TXT:You<SP>can<SP>go<SP>back" + "\n";
retcode = iimPlay(macro);
while (retcode < 1) {
macro = "CODE:";
macro += "SET !ERRORIGNORE NO" + "\n";
macro += "TAG POS=1 TYPE=A ATTR=TXT:You<SP>can<SP>go<SP>back" + "\n";
retcode = iimPlay(macro);
}
link return error in imacros

iMacros -- If site loads, wait; otherwise ignore

i am using Imacros Add-on with Firefox
this is My Code
VERSION BUILD=8961227 RECORDER=FX
SET !ERRORIGNORE YES
SET !TIMEOUT_TAG 1
SET !TIMEOUT_STEP 1
SET !TIMEOUT_PAGE 15
SET !REPLAYSPEED FAST
TAB T=1
'open the proxy file
SET !DATASOURCE C:\proxy.csv
SET !DATASOURCE_COLUMNS 1
SET !LOOP 1
SET !DATASOURCE_LINE {{!LOOP}}
PROXY ADDRESS={{!COL1}}
URL GOTO=http://mywebsite.com
WAIT SECONDS=25
Now i want that if URL loads and not Gives Error than it Wait 25 Seconds, and if URL does not Load Then it Ends.
This issue is very simple to apply the JavaScript Scripting Interface. Let’s suppose that if your URL gives an error, the title of the web-page matches some text. (I assumed ‘Error’ or ‘Problem’. Certainly, you can use another text or tag.) Here is a macro which does what you need:
SET !TIMEOUT_STEP 1
SET !TIMEOUT_PAGE 15
SET !REPLAYSPEED FAST
'open the proxy file
SET !DATASOURCE C:\proxy.csv
SET !LOOP 1
SET !DATASOURCE_LINE {{!LOOP}}
TAB T=1
PROXY ADDRESS={{!COL1}}
SET !ERRORIGNORE YES
URL GOTO=http://mywebsite.com
SET !ERRORIGNORE NO
SET !EXTRACT_TEST_POPUP NO
TAG POS=1 TYPE=TITLE ATTR=TXT:* EXTRACT=TXT
SET E EVAL("('{{!EXTRACT}}'.match(/Error|Problem/i)) ? MacroError('THE PAGE NOT LOADED ! ') : '';")
WAIT SECONDS=25

imacros take from multi files in DATASOURCE loop

I have imacros firefox Add-ons that takes data from one csv file. But i need to tell imacros to take data from multi files as loop
here my script is take from 1.csv
but I've 2.csv & 3.csv & 4.csv and more.. i need it to take from them as loop like if i run play imacros will take data from 1.csv once they finish start again but from 2.csv ..etc like that.. with the same rule for all.. also the files all are the same columns same data but cannot merge them as one file.csv
SET !DATASOURCE /Users/almishal/Desktop/imacros/new/1.csv
SET !DATASOURCE_COLUMNS 18
SET !LOOP 1
SET !DATASOURCE_LINE {{!LOOP}}
TAB CLOSEALLOTHERS
TAG POS=1 TYPE=IMG ATTR=SRC:http://www.example.com/vb/sty1/buttons/newthread.gif
TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:vbform ATTR=NAME:subject CONTENT={{!COL2}}
SET !VAR1 EVAL("'{{!COL4}}'.replace(/^965/gi,"")")
SET PRICE {{!COL5}}
SET !VAR3 EVAL("'price:<SP>{{PRICE}}<SP>dollar'.replace(/price:<SP>0+<SP>dollar/,"")")
TAG POS=1 TYPE=TEXTAREA FORM=NAME:vbform ATTR=ID:vB_Editor_001_textarea CONTENT=[CENTER]{{!COL3}}<BR>{{!VAR3}}<BR><SP>my<SP>number<SP>{{!VAR1}}<SP><BR><BR>{{!VAR2}}<BR>[/CENTER]
TAG POS=1 TYPE=INPUT:SUBMIT FORM=NAME:vbform ATTR=ID:vB_Editor_001_save
TAG POS=1 TYPE=AREA ATTR=HREF:http://www.example.com/vb/index.php
ONDIALOG POS=1 BUTTON=OK CONTENT=
TAG POS=1 TYPE=A ATTR=TXT:log<SP>out
You can get the most elegant solution with the help of the JavaScript Scripting Interface. But here I suggest a one using only ‘iim’s. So, create e.g. ‘macro1.iim’ for ‘1.csv’, ‘macro2.iim’ for ‘2.csv’ etc. The code of the ‘macro1.iim’ will be as follows:
' number of lines in csv-files
SET linesInFile 1
SET !LOOP EVAL(2-{{linesInFile}})
SET !DATASOURCE /Users/almishal/Desktop/imacros/new/1.csv
SET !DATASOURCE_LINE EVAL({{!LOOP}}+{{linesInFile}}-1)
TAB CLOSEALLOTHERS
' ... (your code here)
' next macro to play
SET next "macro2"
SET S EVAL("('{{!LOOP}}' == 1) ? 'imacros://run/?m={{next}}.iim' : 'javascript:undefined;';")
URL GOTO={{S}}
I believe that you’ve caught the idea and will make necessary corrections to ‘macro2.iim’ etc. (There won’t be a ‘next macro to play’ block in the last macro.) In order to play this chain you should run only ‘macro1.iim’ in usual (NOT loop) mode. Also note that I supposed that all macros are in the root of the default ‘iMacros’ folder for them.

iMacros: URL GOTO = null, skip?

I have a long sequence of code, that visit a website, logins in, posts content from a .csv and saves information, logs out and visit the next website. It's all hard-coded.
What is the easiest way of skipping parts of the code?
I have 60 websites. Example:
Right now, I don't want the macro visiting website 5, 6, 7, 9, 10, 22, 26, 35, 40, 45, 50, 59.
In one hour, I don't want the macro visiting website 4, 5 , 9, 10, 19, 30, 31, 49 and 50.
Example how my code looks:
' First Website
TAB T=1
SET !DATASOURCE test.csv
SET !DATASOURCE_COLUMNS 2
SET !LOOP 2
SET !DATASOURCE_LINE {{!LOOP}}
SET !ERRORIGNORE YES
SET !TIMEOUT_PAGE 2
URL GOTO=http://liu.com
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:mortalSubmit ATTR=NAME:title CONTENT={{!COL1}}
TAG POS=1 TYPE=TEXTAREA FORM=ID:mortalSubmit ATTR=ID:post CONTENT={{!COL2}}
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:mortalSubmit ATTR=ID:submit
ADD !EXTRACT {{!URLCURRENT}}
ADD !EXTRACT {{!COL1}}
ADD !EXTRACT {{!COL2}}
TAG POS=1 TYPE=DIV ATTR=CLASS:success EXTRACT=TXT
TAG POS=1 TYPE=DIV ATTR=CLASS:error EXTRACT=TXT
SAVEAS TYPE=EXTRACT FOLDER=* FILE=report.csv
TAG POS=2 TYPE=A ATTR=TXT:Log<SP>out
WAIT SECONDS=3
' Second Website
TAB T=1
SET !DATASOURCE test.csv
SET !DATASOURCE_COLUMNS 2
SET !LOOP 3
SET !DATASOURCE_LINE {{!LOOP}}
SET !ERRORIGNORE YES
SET !TIMEOUT_PAGE 2
URL GOTO=http://kang.com/admin
TAG POS=1 TYPE=A ATTR=TXT:Add<SP>New
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:combatSubmit ATTR=NAME:title CONTENT={{!COL1}}
TAG POS=1 TYPE=TEXTAREA FORM=ID:combatSubmit ATTR=ID:post CONTENT={{!COL2}}
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:combatSubmit ATTR=ID:submit
ADD !EXTRACT {{!URLCURRENT}}
ADD !EXTRACT {{!COL1}}
ADD !EXTRACT {{!COL2}}
TAG POS=1 TYPE=DIV ATTR=CLASS:updated EXTRACT=TXT
SAVEAS TYPE=EXTRACT FOLDER=* FILE=report.csv
TAG POS=2 TYPE=A ATTR=TXT:Exit
WAIT SECONDS=3
This works perfectly for me.
So, in the example above how do I make the code skip First Website and straight go for the Second Website? I know I can make the first parts of the code a comment and change SET !LOOP from 3 to 2. It works, but I can't do this with 60 websites.
I was thinking..
I have a seperate .csv with URL information:
URL,Name
http://liu.com,LIU.COM
http://kang/admin.com,KANG.COM
And in the code:
SET !DATASOURCE urls.csv
' First Website
SET !LOOP 2
URL GOTO={{!COL1}}
' Second Website
SET !LOOP 3
URL GOTO={{!COL1}}
And somehow make IF col1 = null GOTO COMMAND LINE x, or something!
Or just let type in dasjdkaskasdasasdasg.com in col1 and let it run trough, though this would be a timewaster. Does it exist a better solution?
SET !LOOP is also a problem. Is it possible to make the first !SETLOOP 2 and every other that comes next !SETLOOP previous+1
Best regards,
Liu Kang
UPPDATED: Impossible to achive this just by using IIM. Tried long with EVAL to make SETLOOP go up, but this is very limited. See updated comment.
Summary from comment:
Splitted up the code into one .iim-file per website
All .iim-files have a unique SETLOOP and imports data from the same .CSV
A Javascript file execute each .iim-file
Working on a solution to make the Javascript execute the .iim-files only when COL3 in the .CSV contains data, problem now is to make the Javascript import data from the .CSV when jQuery isn't allowed when using iMacros.
here is code example on
how to import .csv data to Javascript when jQuery is not allowed with
iMacros?
var checksubj,scrape;
checksubj = "CODE:";
checksubj += "SET !DATASOURCE example.csv" + "\n";
checksubj += "SET !DATASOURCE_COLUMNS 1" + "\n";
checksubj += "SET !DATASOURCE_LINE {{line}}" + "\n";
checksubj += "SET !extract {{!col1}}" + "\n";
iimSet("line",1);
iimPlay(checksubj);
scrape=iimGetLastExtract(0);