IF THEN ELSE commands Help

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
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
stergosfrag
Posts: 4
Joined: Sat Apr 11, 2020 8:32 pm

IF THEN ELSE commands Help

Post by stergosfrag » Sat Apr 11, 2020 9:08 pm

Hello, I am currently need some help with a code.

What I am trying to do is to click a tag on a website only if a specific CSS is exists.
I am using windows 10, and latest version of chrome and imacros.

As an example this code is working but it is click only the first tag of the webpage:

Code: Select all

TAG POS={{!LOOP}} TYPE=DIV ATTR=CLASS:"item-options text-center watch-btn"
Example:

IF
badge.badge-primary
THEN
TAG POS={{!LOOP}} TYPE=DIV ATTR=CLASS:"item-options text-center watch-btn"

Here is a code which I found on this forum, I change it to my case but still can't make it work:

Code: Select all

VERSION BUILD=1005 RECORDER=CR
SET !ERRORIGNORE YES
SET !TIMEOUT_STEP 0
SET !EXTRACT_TEST_POPUP NO
TAG POS={{!LOOP}} TYPE=DIV ATTR=CLASS:"item-options text-center watch-btn"
SET !VAR1 EVAL("var s='{{!EXTRACT}}'; if(s=='<class=\"badge-primary\" "></>'){s='<class=\"item-options text-center watch-btn\" "></>';} else {s='';}; s;")
But I get: SyntaxError: wrong format of SET command at line 6

Can someone help please?
Thanks in advance!
chivracq
Posts: 9368
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: IF THEN ELSE commands Help

Post by chivracq » Sun Apr 12, 2020 12:48 am

stergosfrag wrote:
Sat Apr 11, 2020 9:08 pm
Hello, I am currently need some help with a code.

What I am trying to do is to click a tag on a website only if a specific CSS is exists.

I am using

Code: Select all

windows 10, and latest version of chrome and imacros.
As an example this code is working but it is click only the first tag of the webpage:

Code: Select all

TAG POS={{!LOOP}} TYPE=DIV ATTR=CLASS:"item-options text-center watch-btn"
Example:

Code: Select all

IF
badge.badge-primary
THEN
TAG POS={{!LOOP}} TYPE=DIV ATTR=CLASS:"item-options text-center watch-btn"
Here is a code which I found on this forum, I change it to my case but still can't make it work:

Code: Select all

VERSION BUILD=1005 RECORDER=CR
SET !ERRORIGNORE YES
SET !TIMEOUT_STEP 0
SET !EXTRACT_TEST_POPUP NO
TAG POS={{!LOOP}} TYPE=DIV ATTR=CLASS:"item-options text-center watch-btn"
SET !VAR1 EVAL("var s='{{!EXTRACT}}'; if(s=='<class=\"badge-primary\" "></>'){s='<class=\"item-options text-center watch-btn\" "></>';} else {s='';}; s;")
But I get: SyntaxError: wrong format of SET command at line 6

Can someone help please?
Thanks in advance!

FCIM...! :mrgreen: (Read my Sig...)
=> "latest" doesn't mean much on a Tech Forum, and won't mean anything at all anymore in a few weeks/months/years, simply mention the (exact) Versions..., and 'Free'/'PE' is missing also...

The easiest way to implement your Conditional Logic is to use 'Relative Positioning', and if the 'IF' Condition is on the Element that you need to click/tag itself or some Containing Element, then you'll need 'Double Relative Positioning'... :idea:

It could be done like you are trying to do, but that will be a bit more "complex", and you need to extract "something", ah-ah...!, your Script doesn't contain any 'EXTRACT'...! :P
And your 'EVAL()' Statement is probably "wrong" because of not escaped Double Quotes, that also look a bit orphan to me...
And I don't know what you'll get in the 'EXTRACT', but it certainly won't look like (only) the String you are trying to compare..., and to be honest, I don't really understand what you are trying to do in your 'EVAL()'... :?
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
stergosfrag
Posts: 4
Joined: Sat Apr 11, 2020 8:32 pm

Re: IF THEN ELSE commands Help

Post by stergosfrag » Sun Apr 12, 2020 6:08 am

Hello sir, thanks for reply.
windows 10, version of chrome (Version 81.0.4044.92 (Official Build) (64-bit)) and imacros Version 10.0.5 Free edition.

I could provide URL but you have to create account to see the webpage.
What I am trying to do is to click the text "Watch" only at videos I have not watched.

Code A: Here is the HTML of 1 tag which I want imacros to click the text "Watch":

Code: Select all

<div class="card-content">
                            <div class="text-center">
                                    <img src="https://img.youtube.com/vi/FyW_QMkStzF/mqdefault.jpg" class="img-fluid" alt="img-placeholder">
                            </div>
                            <div class="card-body">
                                <div class="item-wrapper">
                                    <div class="item-rating video-status">
                                        <div class="badge badge-primary badge-md">
                                            New <i class="feather icon-star ml-25"></i>
                                        </div>
                                    </div>
                                    <div>
                                        <h6 class="item-price video-gram">
                                            1
                                        </h6>
                                    </div>
                                </div>
                                <div class="item-name">
                                    <a>
                                        Video Title
                                    </a>
                                </div>
                                <div>
                                    <p class="item-description">
                                    </p>
                                </div>
                            </div>
                            <div class="item-options text-center watch-btn" data-id="21609">
                                <a href="FyW_QMkStzF" class="" target="_blank">
                                    <div class="cart">
                                        <i class="feather icon-eye"></i><span class="add-to-cart">Watch</span>
                                    </div>
                                </a>
                            </div>
                        </div>
Example:
IF the below class exists on the code:
badge.badge-primary
THEN
Click the text "Watch"

Again, this code is click the text "Watch":

Code: Select all

TAG POS={{!LOOP}} TYPE=DIV ATTR=CLASS:"item-options text-center watch-btn"
But is click all videos of the webpage. Watched and not watched. What I need is to click only to videos I haven't watched.

Code B: And here is the HTML of 1 tag which I want imacros to DON"T click the text "Watch" because I have watch them:

Code: Select all

<div class="card-content">
                            <div class="text-center">
                                    <img src="https://img.youtube.com/vi/Yrzm-F2Wj_c/mqdefault.jpg" class="img-fluid" alt="img-placeholder">
                            </div>
                            <div class="card-body">
                                <div class="item-wrapper">
                                    <div class="item-rating video-status">
                                        <div class="badge badge-success badge-md">
                                            Watched <i class="feather icon-eye ml-25"></i>
                                        </div>
                                    </div>
                                    <div>
                                        <h6 class="item-price video-gram">
                                            
                                        </h6>
                                    </div>
                                </div>
                                <div class="item-name">
                                    <a>
                                        Video Title
                                    </a>
                                </div>
                                <div>
                                    <p class="item-description">
                                    </p>
                                </div>
                            </div>
                            <div class="item-options text-center watch-btn" data-id="21414">
                                <a href="Yrzm-F2Wj_c" class="" target="_blank">
                                    <div class="cart">
                                        <i class="feather icon-eye"></i><span class="add-to-cart">Watch</span>
                                    </div>
                                </a>
                            </div>
                        </div>
As you can see at the above code the class "badge.badge-primary" from the code A HTML is not exists and it's replaced with class "badge badge-success badge-md".

Is there anyway to set imacros to click the text "Watch" only from the code A?
Hope I have provide all the required info.

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

Re: IF THEN ELSE commands Help

Post by chivracq » Sun Apr 12, 2020 2:44 pm

stergosfrag wrote:
Sun Apr 12, 2020 6:08 am
Hello sir, thanks for reply.

Code: Select all

windows 10, version of chrome (Version 81.0.4044.92 (Official Build) (64-bit)) and imacros Version 10.0.5 Free edition.
I could provide URL but you have to create account to see the webpage.
What I am trying to do is to click the text "Watch" only at videos I have not watched.

Code A: Here is the HTML of 1 tag which I want imacros to click the text "Watch":

Code: Select all

<div class="card-content">
                            <div class="text-center">
                                    <img src="https://img.youtube.com/vi/FyW_QMkStzF/mqdefault.jpg" class="img-fluid" alt="img-placeholder">
                            </div>
                            <div class="card-body">
                                <div class="item-wrapper">
                                    <div class="item-rating video-status">
                                        <div class="badge badge-primary badge-md">
                                            New <i class="feather icon-star ml-25"></i>
                                        </div>
                                    </div>
                                    <div>
                                        <h6 class="item-price video-gram">
                                            1
                                        </h6>
                                    </div>
                                </div>
                                <div class="item-name">
                                    <a>
                                        Video Title
                                    </a>
                                </div>
                                <div>
                                    <p class="item-description">
                                    </p>
                                </div>
                            </div>
                            <div class="item-options text-center watch-btn" data-id="21609">
                                <a href="FyW_QMkStzF" class="" target="_blank">
                                    <div class="cart">
                                        <i class="feather icon-eye"></i><span class="add-to-cart">Watch</span>
                                    </div>
                                </a>
                            </div>
                        </div>
Example:
IF the below class exists on the code:
badge.badge-primary
THEN
Click the text "Watch"

Again, this code is click the text "Watch":

Code: Select all

TAG POS={{!LOOP}} TYPE=DIV ATTR=CLASS:"item-options text-center watch-btn"
But is click all videos of the webpage. Watched and not watched. What I need is to click only to videos I haven't watched.

Code B: And here is the HTML of 1 tag which I want imacros to DON"T click the text "Watch" because I have watch them:

Code: Select all

<div class="card-content">
                            <div class="text-center">
                                    <img src="https://img.youtube.com/vi/Yrzm-F2Wj_c/mqdefault.jpg" class="img-fluid" alt="img-placeholder">
                            </div>
                            <div class="card-body">
                                <div class="item-wrapper">
                                    <div class="item-rating video-status">
                                        <div class="badge badge-success badge-md">
                                            Watched <i class="feather icon-eye ml-25"></i>
                                        </div>
                                    </div>
                                    <div>
                                        <h6 class="item-price video-gram">
                                            
                                        </h6>
                                    </div>
                                </div>
                                <div class="item-name">
                                    <a>
                                        Video Title
                                    </a>
                                </div>
                                <div>
                                    <p class="item-description">
                                    </p>
                                </div>
                            </div>
                            <div class="item-options text-center watch-btn" data-id="21414">
                                <a href="Yrzm-F2Wj_c" class="" target="_blank">
                                    <div class="cart">
                                        <i class="feather icon-eye"></i><span class="add-to-cart">Watch</span>
                                    </div>
                                </a>
                            </div>
                        </div>
As you can see at the above code the class "badge.badge-primary" from the code A HTML is not exists and it's replaced with class "badge badge-success badge-md".

Is there anyway to set imacros to click the text "Watch" only from the code A?
Hope I have provide all the required info.

Thanks again, for the help!

Alright, OK, for your FCI..., and hum, CR81 has been released, I see, I thought 'Google' (the Company) were going to hold on to CR80 for "a while", hum, OK...

Then..., hum-hum..., I normally don't help for the Functionality that you want, as it involves Competition and (financial) Gain and the Site you want to automate will probably not be "very happy", and such Scripts actually drive a Cat & Mouse Game with Captcha and (Legit) Anti-Automation Measures, but OK..., you've put some good Quality and Effort in your Thread, so you are a bit "lucky"... :wink:

OK, I had to simplify a bit your 2 HTML Sources for "Watched" and "Not watched", but yep-yep, like I mentioned, (Single) 'Relative Positioning' on your "badge-primary" (or on "video-status") will do the Trick indeed, I would think...

"Watched":

Code: Select all

<div class="card-body">
    <div class="item-wrapper">
        <div class="item-rating video-status">
            <div class="badge badge-success badge-md">
                Watched <i class="feather icon-eye ml-25"></i>
            </div>
        </div>
    </div>
</div>

<div class="item-options text-center watch-btn" data-id="21414">
    <a href="Yrzm-F2Wj_c" class="" target="_blank">
        <div class="cart">
            <i class="feather icon-eye"></i><span class="add-to-cart">Watch</span>
        </div>
    </a>
</div>
"Not watched":

Code: Select all

<div class="card-body">
    <div class="item-wrapper">
        <div class="item-rating video-status">
            <div class="badge badge-primary badge-md">
                New <i class="feather icon-star ml-25"></i>
            </div>
        </div>
    </div>
</div>

<div class="item-options text-center watch-btn" data-id="21609">
    <a href="FyW_QMkStzF" class="" target="_blank">
        <div class="cart">
            <i class="feather icon-eye"></i><span class="add-to-cart">Watch</span>
        </div>
    </a>
</div>
... Then using 'Relative Positioning':

Code: Select all

TAG POS={{!LOOP}} TYPE=DIV ATTR=CLASS:"badge badge-primary badge-md"
TAG POS=R1 TYPE=DIV ATTR=CLASS:"item-options text-center watch-btn"
This one should work also, I think...:

Code: Select all

TAG POS={{!LOOP}} TYPE=DIV ATTR=CLASS:"item-rating video-status"&&TXT:*New*
TAG POS=R1 TYPE=DIV ATTR=CLASS:"item-options text-center watch-btn"
The Wildcards around "New" are probably not needed...

And for both, if the 'TAG' on the 'Anchor' "does something" you don't want from clicking on it, you can disable that Click by adding some "fake" 'EXTRACT', that's it...

And hum, I'm not sure about your "POS={{!LOOP}}", you might have to use "POS=1" instead..., as the Status will change, once a Video has been watched...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
stergosfrag
Posts: 4
Joined: Sat Apr 11, 2020 8:32 pm

Re: IF THEN ELSE commands Help

Post by stergosfrag » Sun Apr 12, 2020 3:43 pm

Both codes you send me are working! Thank you so much, really appreciate it!

One more optional question, when I click "Play Loop" on imacros with e.g. 10 max repeatedly and it founds only 3 tags, imacros is still searching for new tags again and again until 10 times which is normal.
Is there any way to stop imacros if no more tags found?

Thank you so much again!
Last edited by stergosfrag on Sun Apr 12, 2020 3:59 pm, edited 1 time in total.
chivracq
Posts: 9368
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: IF THEN ELSE commands Help

Post by chivracq » Sun Apr 12, 2020 3:54 pm

stergosfrag wrote:
Sun Apr 12, 2020 3:43 pm
Both codes you send me are working! Thank you so much, really appreciate it!

One more optional question, when I click "Play Loop" on imacros with e.g. 10 max repeatedly and it founds only 3 tags, imacros is still searching for new tags again and again until 10 times which is normal.
Is there any way to stop imacros if no more tags found?

Thank you so much again!

Ouf-ouf...!, you "don't need" to quote yourself again, this doesn't help for Readability of the Thread...! :shock:
=> Can you edit/simplify your previous Post...?

You actually don't "really" need to quote at all, unless you are replying to a specific part of my Reply, or if several (Advanced) Users were participating in your Thread... I do quote systematically because "too many" Users (especially for the Functionality that you want), have a "Tendency" to delete/mutilate their OP/Posts once they've got their Solution/working Script, to prevent the "Competition" from using that Script, or they keep editing their OP as they "progress", which renders such Threads impossible to "follow" for other Users searching the Forum with a similar Case/Scenario, but I don't delete my Posts... :wink:

... => Comply with that "Request" first for me to follow up and to read further... :!:
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
chivracq
Posts: 9368
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: IF THEN ELSE commands Help

Post by chivracq » Sun Apr 12, 2020 4:05 pm

OK, good, Thanks for the Edit/Simplification of your previous Reply... :D

Alright..., glad to hear that "it works"..., I hope you understood the Principle...

And then, to have your Script abort once there are no more available Elements, simply disable/don't use '!ERRORIGNORE', I would think... :idea:

... And you may want to post your Final/Working Script, to finish your Thread "correctly", ... and "useful" for other Users searching the Forum... 8)
Last edited by chivracq on Sun Apr 12, 2020 4:12 pm, edited 1 time in total.
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
stergosfrag
Posts: 4
Joined: Sat Apr 11, 2020 8:32 pm

Re: IF THEN ELSE commands Help

Post by stergosfrag » Sun Apr 12, 2020 4:08 pm

Omg yes you are right I have forgot this at all.
You are awesome! Thank you so much for sharing your knowledge!
chivracq
Posts: 9368
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: IF THEN ELSE commands Help

Post by chivracq » Sun Apr 12, 2020 4:14 pm

stergosfrag wrote:
Sun Apr 12, 2020 4:08 pm
Omg yes you are right I have forgot this at all.
You are awesome! Thank you so much for sharing your knowledge!

Yeah well, you were the one who had "enabled" '!ERRORIGNORE', ah-ah...! :wink:

Hum, I edited my previous Post in the time that you were already replying, about posting your Final Script... :idea:
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
Post Reply