PyautoGUI doesn't give out coordinates for one function while works for another

Viewed 60

I am working on a script that accepts the game of league of legends automatically, then moves to the chat dialogue to call the lane. This worked fine for the click_accept() function, prints out the x,y coordinates perfectly. However the print(pos.x/2,pos.y/2) line for click_chat_dialog() gives me the following error:

AttributeError: 'NoneType' object has no attribute 'x'

Here is my code:

import pyautogui, time, os, logging, sys, random, copy

def imPath(filename):
    return os.path.join('images', filename)


def click_accept():
    if pyautogui.locateOnScreen(imPath('accept_button.png'), confidence=0.8) != None:
        pos = pyautogui.locateCenterOnScreen(imPath('accept_button.png'))
        print(pos.x/2,pos.y/2)
        pyautogui.moveTo(pos.x/2,pos.y/2)
        time.sleep(0.5)
        pyautogui.click(pos.x/2,pos.y/2)

def click_chat_dialog():
    if pyautogui.locateOnScreen(imPath('chat_dialog.png'), confidence=0.6) != None:
        pos = pyautogui.locateCenterOnScreen(imPath('chat_dialog.png'))
        print(pos.x/2,pos.y/2)
        pyautogui.moveTo(pos.x/2,pos.y/2)
        time.sleep(0.5)
        pyautogui.click(pos.x/2,pos.y/2)



while 1:
    click_accept()
    click_chat_dialog()

Here are the images I am using:

Accept Button - https://i.imgur.com/1vW6cp3.png

Chat Dialogue - https://i.imgur.com/PwdXii3.png

Accept Screen - https://i.imgur.com/xOeLTmq.png

Chat Screen - https://i.imgur.com/LfZQCnR.png

1 Answers

NoneType is returned when PyAutoGUI cannot find what you are looking for. I see that you tested to see if the object could be found, but another exception handling may be required for locatecenter...without seeing the actual input (image) of what you are looking for that's about as much as I could say.

import pyautogui, time, os, logging, sys, random, copy

def imPath(filename):
    return os.path.join('images', filename)


def click_accept():
    if pyautogui.locateOnScreen(imPath('accept_button.png'), confidence=0.8) != None:
        pos = pyautogui.locateCenterOnScreen(imPath('accept_button.png'))
        print(pos.x/2,pos.y/2)
        pyautogui.moveTo(pos.x/2,pos.y/2)
        time.sleep(0.5)
        pyautogui.click(pos.x/2,pos.y/2)

def click_chat_dialog():
    if pyautogui.locateOnScreen(imPath('chat_dialog.png'), confidence=0.6) != None:
        try:
            pos = pyautogui.locateCenterOnScreen(imPath('chat_dialog.png'))
        except TypeError:
            #perform some other action because pos was not found..
        print(pos.x/2,pos.y/2)
        pyautogui.moveTo(pos.x/2,pos.y/2)
        time.sleep(0.5)
        pyautogui.click(pos.x/2,pos.y/2)



while 1:
    click_accept()
    click_chat_dialog()
Related