How to search when the target is located below of the page (scrolling needed)?

Asked by obaskirt

I am trying this case:
I opened facebook
I can not see the jpg in current screen (cuz it is loacated under current screen/page)
then I want to find a jpg which is located down/below of the page.

I wrote this code. It works perfect but does anybody know faster method?

Here is my code:

reg=Region(Region(274,100,526,605))

if reg.exists("ZLQQiu-1.png"):
    popup("Bravo")
    exit()
mouseMove("1323985068132-1.png")
mouseDown((Button.LEFT))
mouseLocation=Env.getMouseLocation()
dd=90

#Method-1
while not reg.exists("ZLQQiu.png"):
    mouseMove(Location(mouseLocation.x,(mouseLocation.y+dd)))
    dd=dd+90
    if exists("ZLQQiu.png"):
        popup("Congrats!")
        exit()

#Method-2
#while not reg.exists("ZLQQiu.png"):
# wheel(Location(mouseLocation),WHEEL_DOWN,3)
# if exists("ZLQQiu.png"):
# popup("Congrats!")
# break

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

I think using the wheel function is the fastest and most flexible feature to use in these cases.

I would suggest the following improvements:

hover("some_image_inside_window.png")
mouseLocation = Env.getMouseLocation()
maxSteps = 1000
steps = 3
while maxSteps > 0:
    if reg.exists("ZLQQiu.png", 0):
         popup("Congrats!")
         break
    wheel(mouseLocation,WHEEL_DOWN,steps)
    maxSteps -= steps

--- we go to somewhere in the window using hover(), to get a starting point for the scroll operation.
--- we loop until the image is found, but a max number of steps to avoid infinite looping
--- we use exists(image, 0), to save time: only one search try is done. Without the 0 as 2nd parameter, each unsuccessful search would wait 3 seconds.
--- we only search once inside the loop (in your case, each unsuccessful search would have taken 6 seconds). Each step now should take about 0.5 seconds

--- Take Care: depending on the stepping and the speed of scrolling a short wait() might be needed after the wheel():
    wheel(mouseLocation,WHEEL_DOWN,steps); wait(0.5)

Revision history for this message
obaskirt (onur-baskirt) said :
#2

Well done Raiman.

I did a benchmark with respect to two methods. I shared two code snippets. The benchmark shows that you are right! Whell method is faster and reliable.

Benchmark Results:
Method-1: Whell Method finds in : 11.3 seconds
Method-2: Drag and Drop Method finds in : 19.3 seconds

###Method-1: Whell Method###
reg = Region(Region(x,y,w,z))

hover("1324231594260.png")
mouseLocation = Env.getMouseLocation()
maxSteps = 1000
steps = 3
while maxSteps > 0:
    if reg.exists("ZLQQiu.png", 0):
         popup("Congrats!")
         exit()
    print "maxSteps: ", maxSteps
    wheel(Location(mouseLocation),WHEEL_DOWN,3)
    wait(0.5)
    maxSteps -= steps

###Method-2: Drag and Drop Method###
reg = Region(Region(x,y,w,z))

hover("1323985068132-1.png")
mouseDown((Button.LEFT))
mouseLocation=Env.getMouseLocation()
maxSteps=10000
steps=90

while maxSteps > 0:
    if exists("ZLQQiu.png"):
        popup("Congrats!")
        exit()
    mouseMove(Location(mouseLocation.x,(mouseLocation.y+steps)))
    steps+=90
    maxSteps-=steps

Revision history for this message
obaskirt (onur-baskirt) said :
#3

Thanks RaiMan, that solved my question.

Revision history for this message
obaskirt (onur-baskirt) said :
#4

Sorry for typo. It should be "wheel" not "whell"

Revision history for this message
obaskirt (onur-baskirt) said :
#5

After comment out the wait(0.5) statement. Wheel method finds in 8.7 seconds. Its quite faster than drag and drop. Thank you RaiMan.

Revision history for this message
RaiMan (raimund-hocke) said :
#6

Thanks for the feedback and the valuable input.

--- short wait() after wheel()
or other mouse/keyboard actions, that change the screen content:
This is in fact only needed, if the change of the screen content is too slow, so that the next search comes to fast (which might be the case with exists(img,0) ).

So in your case, the wait() seems not to be needed and makes the whole thing even faster.