pyautogui:可以使用布尔值true / false来定位LocateOnScreen吗?

时间:2018-12-05 20:47:19

标签: python pyautogui

编辑:问题似乎源于以下几行:

apply = pyautogui.locateOnScreen("apply.png")
if apply is not None:
    x, y = pyautogui.locateCenterOnScreen('apply.png')
    pyautogui.click(x,y)

是否有制作它的方法,以便您可以运行if语句来检查图像是否存在?

警告:python中的新功能

我的目标是使用最新版本的firefox(完全最大化的窗口)自动在确实的网站上执行工作。到目前为止,我只在一页纸上工作,代码并不完美。它开始,单击第一个工作职位,在右侧找到“立即申请”橙色按钮,单击该按钮将打开一个表单,填写该表单,上传简历,单击继续,然后单击Apply(提交)以提交最终表单。

我遇到2个问题:

  1. 我不确定如何告诉程序:如果此图像确实存在于页面上的任何位置。相反,我有if apply is not None:,但我不确定它是否有效。我只知道if applynow == None:在工作。

  2. 我在一个职位上进行了测试,应该提交它,但给我一个错误消息“ TypeError:无法解压缩不可迭代的NoneType对象”,这意味着该图像不存在。很好,但是那部分在else语句中,而不是在初始if语句中。最初的if语句应该首先触发...因此应该只单击apply而不通过else语句,因此要么未检测到“ apply”的图像,要么python需要通过else语句运行在执行之前弄乱了执行,如果有任何意义的话。我不确定我要面对哪个问题,因此可以提供任何帮助。

错误:

    x, y = pyautogui.locateCenterOnScreen('continue2.png')
TypeError: cannot unpack non-iterable NoneType object

链接:https://www.indeed.ca/jobs?q=engineer&l=Vancouver%2C%20BC&start=40&vjk=64c9d891c652bd2b

import pyautogui, time

pyautogui.PAUSE = 1
pyautogui.FAILSAFE = True
time.sleep(5)
pyautogui.click(863,330)

applynow = pyautogui.locateOnScreen("applynow.png")
confirmyourapplication = pyautogui.locateOnScreen("confirmyourapplication.png")
confirmonnextpage = pyautogui.locateOnScreen("confirmonnextpage.png")
howmanyyears = pyautogui.locateOnScreen("howmanyyears.png")
apply = pyautogui.locateOnScreen("apply.png")

for i in range (0, 9):
    if applynow == None:
        pyautogui.scroll(-425)
        x, y = pyautogui.position()
        pyautogui.click(x,y)
    else:
        x, y = pyautogui.locateCenterOnScreen('applynow.png')
        pyautogui.click(x,y)
        pyautogui.press('tab')
        pyautogui.press('tab')
        pyautogui.typewrite('Jake Williams', 0.25)
        pyautogui.press('tab')
        pyautogui.typewrite('jwilliam02@hotmail.com', 0.25)
        pyautogui.press('tab')
        pyautogui.typewrite('719-985-3546', 0.25)
        x, y = pyautogui.locateCenterOnScreen('browse.png')
        pyautogui.click(x,y)
        pyautogui.typewrite('resume', 0.25)
        pyautogui.press('enter')
        x, y = pyautogui.locateCenterOnScreen('continue.png')
        pyautogui.click(x,y)
        time.sleep(2)
        ## insert function here
        if apply is not None:
            x, y = pyautogui.locateCenterOnScreen('apply.png')
            pyautogui.click(x,y)
            x, y = pyautogui.locateCenterOnScreen('close.png')
            pyautogui.click(x,y)
        else:
            for j in range (0, 3):
                if howmanyyears is not None:
                    x, y = pyautogui.locateCenterOnScreen('howmanyyears.png')
                    pyautogui.click(x,y)
                    pyautogui.typewrite('3', 0.25)
            x, y = pyautogui.locateCenterOnScreen('continue2.png')
            pyautogui.click(x,y)
            time.sleep(2)
            x, y = pyautogui.locateCenterOnScreen('apply.png')
            pyautogui.click(x,y)
            x, y = pyautogui.locateCenterOnScreen('close.png')
            pyautogui.click(x,y)

2 个答案:

答案 0 :(得分:0)

尝试使用此代码:

#code before if statement here  
#the code below is an if statement replacement  
#tab button on my keyboard doesn't work, replacing it with spaces  
try:  
    x, y = pyautogui.locateCenterOnScreen('continue2.png')  
except TypeError:  
    #insert code to run if the image isn't located  
else:  
    #insert code to run if the image is located  
finally:  
    #insert rest of program here

答案 1 :(得分:0)

当我找不到图像时,我通常会收到此错误。

TypeError: cannot unpack non-iterable NoneType object

尽量确保图像在屏幕上没有障碍物。