[Python] Sikuli: for loop/if else statement not working correctly

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following code in Sikuli that does copy and paste tasks. I want it to execute the actions inside "if not exists" when caption.png does not exist, but sometimes even if it doesn't, it is still trying to find it and returns an error that says "can't find caption.png".

    for idx in range(1, 77):
    if not exists("caption.png"):
    click("1406907972716.png")
    type(Key.PAGE_DOWN)
    print "skip" + str(idx)
    else:
    click("caption.png")
    type(Key.F2)
    type("a", KeyModifier.CTRL)
    type("c", KeyModifier.CTRL)
    click("CLICKBOX-4.PNG")
    click("BOX2-4.PNG")
    type("a", KeyModifier.CTRL)
    type(Key.DELETE)
    type("v", KeyModifier.CTRL)
    type(str(idx))
    type(Key.HOME)
    for i in range(25):
    type(Key.DELETE)
    click("1406907972716.png")
    type(Key.PAGE_DOWN)


    I tried the following and it worked, but something else does not work properly: sometimes it randomly jumps one idx. Can't figure out why:

    for idx in range(1, 77):
    if not exists("caption.png"):
    click("1406907972716.png")
    type(Key.PAGE_DOWN)
    print "skip" + str(idx)
    else:
    if exists("caption.png"):
    click("caption.png")
    type(Key.F2)
    type("a", KeyModifier.CTRL)
    type("c", KeyModifier.CTRL)
    click("CLICKBOX-4.PNG")
    click("BOX2-4.PNG")
    type("a", KeyModifier.CTRL)
    type(Key.DELETE)
    type("v", KeyModifier.CTRL)
    type(str(idx))
    type(Key.HOME)
    for i in range(25):
    type(Key.DELETE)
    click("1406907972716.png")
    type(Key.PAGE_DOWN)

    Continue reading...

Compartilhe esta Página