Loop script keeps clicking on same image instead of moving through Array

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
rjw524
Pro Scripter
Posts: 104
Joined: Wed May 09, 2012 9:45 pm
Location: Michigan

Loop script keeps clicking on same image instead of moving through Array

Post by rjw524 » Tue Mar 07, 2023 3:03 am

Hi,

I've written a script for a dynamically generated webpage that brings up a different number of records depending on the customer name you click.

Customer A may only have 3 records. Customer B may have 8 records. Customer Z may have 25 and so on.
There are thousands of customers.

What doesn't change is a unique javascript "Edit" image to the left of each record.

So for Customer A there's an "Edit" image to the left of each of the 3 records. For Customer Z there's 25 "Edit" images, etc.

I need to click EACH "Edit" image to open a page and pull data from it.

I created a script that looks at the page and puts the "Edit" images in an array and is supposed to click each Edit image.

PROBLEM: My script seems to click the first "Edit" image it sees and instead of clicking the next one and the next one and so on until it's finished, it clicks the same first image over and over.

I even included a MDL>%EditFound% to see if the array itself is the problem and it isn't. It's finding multiple identical images on the page. The problem appears to be in my script and how I'm looping the loop.

IMPORTANT: Clicking each "Edit" image is the only way to get to the data I need. Unfortunately, there is no workaround.

I've included my script to see if anyone here can tell me what I did wrong in constructing the loop.

Thanks!

rjw524

Code: Select all

SetFocus>Microsoft​ Edge*

Label Start
Wait 1.0

//Find and Left Click Center of EDIT_IMAGE
FindImagePos>%BMP_DIR%\image_6.bmp,SCREEN,0.7,1,XArr1,YArr1,EditFound,EXACT
If>EditFound>0
 Let>k=0
 Repeat>k
  Letk=k+1
  Wait 0.05
  MouseMove>XArr1_%k%,YArr1_%k%
  wait 0.05
  LClick
  Wait 2.0
    
    //Find and Move Mouse Middle Right of DATA1
    FindImagePos>%BMP_DIR%\image_3.bmp,SCREEN,0.7,8,XArr,YArr,NumFound,CCOEFF
    If>NumFound>0
    MouseMove>XArr_0,YArr_0
    Endif

    Wait 2.0

    //Find and Left Click Center of DATA2
    FindImagePos>%BMP_DIR%\image_4.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
    If>NumFound>0
    MouseMove>XArr_0,YArr_0
    LClick
    Endif

    Wait 1.0
  Until>k={%EditFound%}
EndIf

mhcha
Junior Coder
Posts: 33
Joined: Sat Jan 01, 2022 11:10 am

Re: Loop script keeps clicking on same image instead of moving through Array

Post by mhcha » Tue Mar 07, 2023 6:35 am

Hello,
for webpages, chromebrowser automation can help. If you can post an example page I can help.


Helpful URL
https://www.mjtnet.com/manuals/b/v15/in ... ntext=4320

rjw524
Pro Scripter
Posts: 104
Joined: Wed May 09, 2012 9:45 pm
Location: Michigan

Re: Loop script keeps clicking on same image instead of moving through Array

Post by rjw524 » Tue Mar 07, 2023 8:39 am

mhcha wrote:
Tue Mar 07, 2023 6:35 am
Hello,
for webpages, chromebrowser automation can help. If you can post an example page I can help.


Helpful URL
https://www.mjtnet.com/manuals/b/v15/in ... ntext=4320
Thanks, unfortunately it's for work so I can't post an example page. And I don't have version 15 of MS yet so I don't think I can use chromebrowser. (Also we use Microsoft Edge...but I think 15 has Edge browser automation too right?)

Anyway, the FindImagePos should allow me to move through the entire array, correct?

Does the fact that clicking on the "Edit" image in my example opens a new page cause FindImagePos to lose or "forget" the original array?

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1378
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Loop script keeps clicking on same image instead of moving through Array

Post by Dorian (MJT support) » Tue Mar 07, 2023 9:46 am

This is the culprit :

Code: Select all

Letk=k+1
Should be :

Code: Select all

Let>k=k+1
This means k is always 0.

You're missing the chevron (>) here and there. A few of the commands will still be understood by the interpreter without them (and Press doesn't use them at all) but most commands need them.

Also note the first image will be _0 and not _1. So you need to adjust the script accordingly, as if you find 3 images your script would currently click on images 2,3, and non-existent 4.

rjw524 wrote:
Tue Mar 07, 2023 8:39 am
(Also we use Microsoft Edge...but I think 15 has Edge browser automation too right?)
Correct.

rjw524 wrote:
Tue Mar 07, 2023 8:39 am
Anyway, the FindImagePos should allow me to move through the entire array, correct?
Yes, as long as you're using EXACT, which can find multiple images. CCOEFF can only find one.

rjw524 wrote:
Tue Mar 07, 2023 8:39 am
Does the fact that clicking on the "Edit" image in my example opens a new page cause FindImagePos to lose or "forget" the original array?
It wouldn't forget it, as the co-ordinates would still be in the array, but you'd need to make sure that when the script clicks in that position, your intended tab is visible and not the new one, otherwise it'll be clicking in the correct place but the incorrect tab.
Yes, we have a Custom Scripting Service. Message me or go here

rjw524
Pro Scripter
Posts: 104
Joined: Wed May 09, 2012 9:45 pm
Location: Michigan

Re: Loop script keeps clicking on same image instead of moving through Array

Post by rjw524 » Tue Mar 07, 2023 10:11 am

Dorian (MJT support) wrote:
Tue Mar 07, 2023 9:46 am
This is the culprit :

Code: Select all

Letk=k+1
Should be :

Code: Select all

Let>k=k+1
This means k is always 0.

You're missing the chevron (>) here and there. A few of the commands will still be understood by the interpreter without them (and Press doesn't use them at all) but most commands need them.

Also note the first image will be _0 and not _1. So you need to adjust the script accordingly, as if you find 3 images your script would currently click on images 2,3, and non-existent 4.
Thanks SO much! I would have NEVER caught that chevron! You were absolutely right.

Now it's moving through the images but I'm running into the precise problem you mentioned with the image count. It's skipping the 1st image, clicking all the subsequent ones and when it gets to the end I get a:

"Line 38: Invalid Numerical Value for MouseMove Command"

How do I correct that?

Thanks so much again!

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1378
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Loop script keeps clicking on same image instead of moving through Array

Post by Dorian (MJT support) » Tue Mar 07, 2023 10:28 am

By moving let>k=k+1 to the end of the loop (or anywhere in the loop after your mousemove/click)

So the last three lines of your loop would be :

Code: Select all

  Let>k=k+1
  Until>k={%EditFound%}
EndIf
To demonstrate the difference the position of that line makes, try each of these as a standalone script :

0-1-2:

Code: Select all

//0-1-2
Let>EditFound=3

If>EditFound>0
 Let>k=0
 Repeat>k
  mdl>k
  Let>k=k+1
  Until>k=%EditFound%
EndIf

1-2-3:

Code: Select all

//1-2-3
Let>EditFound=3

If>EditFound>0
 Let>k=0
 Repeat>k
  Let>k=k+1
  mdl>k
  Until>k=%EditFound%
EndIf
Also note you don't need the curly braces. We only need those for Complex Expressions/Functions.

Either of these will suffice :

Code: Select all

Until>k=%EditFound%
..or even in this case :

Code: Select all

Until>k=EditFound
Using Variables and When to use Percent Symbols (%variable%)
Yes, we have a Custom Scripting Service. Message me or go here

rjw524
Pro Scripter
Posts: 104
Joined: Wed May 09, 2012 9:45 pm
Location: Michigan

Re: Loop script keeps clicking on same image instead of moving through Array

Post by rjw524 » Tue Mar 07, 2023 10:44 am

Dorian (MJT support) wrote:
Tue Mar 07, 2023 10:28 am
By moving let>k=k+1 to the end of the loop (or anywhere in the loop after your mousemove/click)

So the last three lines of your loop would be :

Code: Select all

  Let>k=k+1
  Until>k={%EditFound%}
EndIf
...
Using Variables and When to use Percent Symbols (%variable%)
AHHH! I understand now! The way I had it the k=k+1 was adding to the image count BEFORE a mousemove or anything for that matter was taking place.

In plain English my original script was basically saying:

"Find this image...ok now go to the NEXT image like that one and do XYZ"

Is that right?
Last edited by rjw524 on Tue Mar 07, 2023 2:23 pm, edited 1 time in total.

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1378
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Loop script keeps clicking on same image instead of moving through Array

Post by Dorian (MJT support) » Tue Mar 07, 2023 11:18 am

Exactly!
Yes, we have a Custom Scripting Service. Message me or go here

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts