user3766432
user3766432

Reputation: 247

iMacros if/else statement how to?

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.

Upvotes: 0

Views: 6006

Answers (2)

Far
Far

Reputation: 147

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

Upvotes: 0

Shugar
Shugar

Reputation: 5299

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=* .

Upvotes: 0

Related Questions