Search code examples
python-3.xwhatsapppyautoguiubuntu-22.04

Ubuntu 22.04: pyautogui.locateOnScreen is returning None. How to solve this?


My OS is Ubuntu 22.04, Python 3.10.4. I am trying to create a code to automate Whatsapp send message.

Have installed latest version of pyautogui.

Following is the code I am running:

import pyautogui as pt
import paperclip as pc
# from pynput.mouse import Controller, Button
from time import sleep

# mouse = Controller()

class WhatsApp:
    def __init__(self, speed=5, click_speed=.3):
        self.speed = speed
        self.click_speed = click_speed
        self.message = ''
        self.last_message = ''

    def nav_green_dot(self):
        try:
            # position = pt.locateOnScreen('clip_pic.png', confidence = .7)
            # position = pt.locateOnScreen('clip_pic.png')
            # print(position)
            print(pt.locateOnScreen('clip_pic.png'))
            
            # pt.moveTo(position[0:2], duration = self.speed)
            # pt.moveRel(-100, 0, duration = self.speed)
        except Exception as e:
            print ('Exception (nav_green_dot): ', e)

wa_bot = WhatsApp(speed = .5, click_speed = .4)
sleep(5)
wa_bot.nav_green_dot()

At print(pt.locateOnScreen('clip_pic.png')) I am getting None. Attached is the picture I am trying to capture. I have already opencv-python installed as well. I also have whatsapp web page opened in a chrome browser. I tested in firefox as well.

The output error is not clear in what direction I should go. What am I missing?

enter image description here


Solution

  • Finding image on screen for only one time may be None you need to check repeatedly for it. And if it is found you can end the loop you are using to find it. You should use python's multithreading for it. here is an updated version of your code

    import pyautogui as pt
    
    import paperclip as pc
    
    # from pynput.mouse import Controller, Button
    from time import sleep
    import threading
    # mouse = Controller()
    
    FOUND_IMAGE = False
    
    def checkFunction():
        global FOUND_IMAGE
        while True:
            img = pt.locateOnScreen('img.png')
            if img != None:
                FOUND_IMAGE = True
                break
    
    
    checkThread = threading.Thread(target=checkFunction) # creating therad
    
    checkThread.start() # starting therad
    class WhatsApp:
        def __init__(self, speed=5, click_speed=.3):
            self.speed = speed
            self.click_speed = click_speed
            self.message = ''
            self.last_message = ''
    
        def nav_green_dot(self):
            try:
                # position = pt.locateOnScreen('clip_pic.png', confidence = .7)
                # position = pt.locateOnScreen('clip_pic.png')
                # print(position)
                print(FOUND_IMAGE)
                
                # pt.moveTo(position[0:2], duration = self.speed)
                # pt.moveRel(-100, 0, duration = self.speed)
            except Exception as e:
                print ('Exception (nav_green_dot): ', e)
    
    wa_bot = WhatsApp(speed = .5, click_speed = .4)
    sleep(5)
    wa_bot.nav_green_dot()
    

    For any queries have a look at this question or this Post