multiple "ifs"

Support for iMacros. The iMacros software is the unique solution for automating every activity inside a web browser, for data extraction and web testing.
Forum rules
iMacros EOL - Attention!

The renewal maintenance has officially ended for Progress iMacros effective November 20, 2023 and all versions of iMacros are now considered EOL (End-of-Life). The iMacros products will no longer be supported by Progress (aside from customer license issues), and these forums will also no longer be moderated from the Progress side.

Thank you again for your business and support.

Sincerely,
The Progress Team

Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
acozme
Posts: 3
Joined: Fri Apr 24, 2020 5:58 pm

multiple "ifs"

Post by acozme » Fri Apr 24, 2020 6:12 pm

Hello! a newbie here! :oops:

I'm trying to fill a form based on one question, if i put one statement works, but i need to put several statements:

Code: Select all

TAG POS=1 TYPE=LABEL ATTR=CLASS:captcha-pregunta EXTRACT=TXT
SET !VAR1 EVAL("var text=\"{{!EXTRACT}}"; if(text=='Si mañana es lunes, ¿qué día es hoy?'){var x = 'domingo';}")
SET !VAR1 EVAL("var text=\"{{!EXTRACT}}"; if(text=='¿Cuántos días tiene junio? Responda con números.') {var x = '31';}")
SET !VAR1 EVAL("var text=\"{{!EXTRACT}}"; if(text=='¿Cuánto es cinco más dos? Responda con números.') {var x = '7';}")
SET !VAR1 EVAL("var text=\"{{!EXTRACT}}"; if(text=='¿Cuál de los siguientes es un color: libro, nariz, verde, queso?') {var x = 'verde';}")
TAG POS=2 TYPE=INPUT:TEXT ATTR=CLASS:"form-control texto-corto" CONTENT={{!VAR1}}
That code doesn't work, that returns !VAR1 == !EXTRACT

Any help is welcome
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: multiple "ifs"

Post by chivracq » Fri Apr 24, 2020 6:37 pm

acozme wrote:
Fri Apr 24, 2020 6:12 pm
Hello! a newbie here! :oops:

I'm trying to fill a form based on one question, if i put one statement works, but i need to put several statements:

Code: Select all

TAG POS=1 TYPE=LABEL ATTR=CLASS:captcha-pregunta EXTRACT=TXT
SET !VAR1 EVAL("var text=\"{{!EXTRACT}}"; if(text=='Si mañana es lunes, ¿qué día es hoy?'){var x = 'domingo';}")
SET !VAR1 EVAL("var text=\"{{!EXTRACT}}"; if(text=='¿Cuántos días tiene junio? Responda con números.') {var x = '31';}")
SET !VAR1 EVAL("var text=\"{{!EXTRACT}}"; if(text=='¿Cuánto es cinco más dos? Responda con números.') {var x = '7';}")
SET !VAR1 EVAL("var text=\"{{!EXTRACT}}"; if(text=='¿Cuál de los siguientes es un color: libro, nariz, verde, queso?') {var x = 'verde';}")
TAG POS=2 TYPE=INPUT:TEXT ATTR=CLASS:"form-control texto-corto" CONTENT={{!VAR1}}
That code doesn't work, that returns !VAR1 == !EXTRACT

Any help is welcome

CIM for me to have a look, read my Sig... and the Forum Rules that you didn't read... :idea:

Hum, I already see directly one "obvious" Mistake..., and possibly another one, but I'll do some "Thinking" once you'll have mentioned your FCI... Hum..., and "do it correctly and directly", I won't be asking several times, ... I usually don't help for Captcha... 8)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
acozme
Posts: 3
Joined: Fri Apr 24, 2020 5:58 pm

Re: multiple "ifs"

Post by acozme » Fri Apr 24, 2020 6:50 pm

Sorry about missing the info:

iMacros free 12.6.505.4567 for Internet Explorer
IE 11.778.18362.0
Windows 10 64 bits
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: multiple "ifs"

Post by chivracq » Fri Apr 24, 2020 8:56 pm

acozme wrote:
Fri Apr 24, 2020 6:50 pm
Sorry about missing the info:

Code: Select all

iMacros free 12.6.505.4567 for Internet Explorer
IE 11.778.18362.0
Windows 10 64 bits

OK, perfect for your FCI... :D
(And next time you open a Thread, include that Info directly in your OP, preferably at the very top, I usually don't read and certainly don't answer if that Required Info is not mentioned...)

>>>

Alright..., hum, first your "obvious Mistake" is that both Double Quotes around the '{{!EXTRACT}}' Var in your 'EVAL()' Statements both need to be Escaped each time, not only the 1st one, ah-ah...!
And instead of using 'Escaped Double Quotes', you can also 'Single Quotes'... that don't need to be Escaped, that's the Syntax I always use myself... :idea:

>>>

Then you have 4x 'EVAL()' Statements, but because they all use the same "!VAR1" Var, then hum..., for iMacros, only the last one "really" gets executed and retained for later Reuse in your 'TAG' Statement at the end of your Script..., which is obviously not the Behaviour that you want...
They all 4 get "executed", but each time the "next one" replaces the Result from the previous one, as you keep using the same Var..., and in the end, only the last one "counts"...

There are 3 Methods to achieve the Functionality that you want:
- Method 1: :idea:
=> if else if else if else if else... etc...

- Method 2: :idea:
=> 'switch'/'case'/'break'.

- Method 3: :idea:
'EVAL()' + '!Var1' x times, a bit like you were doing, but you need to reuse (the previous) '!VAR1' inside every 'EVAL()' Statement from the 2nd Instance to keep building your if + if + if etc...

Methods 1 & 2 will produce ONE (very) long 'EVAL()' Statement, especially with your long Strings like '¿Cuál de los siguientes es un color: libro, nariz, verde, queso?' etc..., but it works and is easy to debug with just 1 'PROMPT' to follow your Vars... 8)
I have one Method 1 'EVAL()' Statement in several of my Scripts that I still use daily, and only that Multiple 'if' 'EVAL()' Statement is 300Kb long, oops...! :twisted: I made it 8 or 10 years ago, when I started using JS in 'EVAL()', and yep, it could be shortened drastically, I vaguely tried once about 5 years ago, but was too much "Thinking" and I gave up after 1h and never tackled it again..., mostly because it still works perfectly, and I never had to ask for any Help for any of my Scripts, or I would feel a mini-little-bit "ashamed" about this one, ah-ah...! :oops:

Method 3 is normally not the "Best Practice" in my Opinion, and not easy to debug, or you need several 'PROMPT' Statements (one for each '!VAR1'), but it produces short 'EVAL()' Statements that don't require scrolling horizontally for 10 or 20 Screens ah-ah...!, and because of the 'Free' iMacros Version you are using, which only supports 3 Vars, hum..., that might actually be the "best Option" for you, after all... :|
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
acozme
Posts: 3
Joined: Fri Apr 24, 2020 5:58 pm

Re: multiple "ifs"

Post by acozme » Fri Apr 24, 2020 10:52 pm

Thanks a lot chivracq!

I used the if else if else if else method and works perfect :D

Thanks again!
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: multiple "ifs"

Post by chivracq » Fri Apr 24, 2020 10:55 pm

acozme wrote:
Fri Apr 24, 2020 10:52 pm
Thanks a lot chivracq!

I used the if else if else if else method and works perfect :D

Thanks again!

Alright, good-good...! And can you share that Script then, as an Example, to make this Thread also useful for other Users...? 8)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
Post Reply