Search code examples
pythonautomationwhatsapppyautogui

How to add an exception in pyautogui


I created a code using pyautogui to send messages automatically in whatsapp, but there is a problem, I'm using a purchased database so some of the phones don't exist or aren't in whatsapp, how could I add this exception in pyautogui? when the phone exists, the program runs normally and clicks on starting the conversation as programmed, but when it does not exist, instead of sending the programmed message to the contact, it sends it to the group where the contacts are

import pyautogui
import pyperclip
import time
import random
import pygame

pyautogui.pause = 2

text = ("""message
message
message """)
pyautogui.press('win')
pyautogui.write('brave')
pyautogui.press('enter')
time.sleep(2)
pyautogui.hotkey('ctrl','t')
pyperclip.copy('https://web.whatsapp.com/')
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')
time.sleep(10)

x = 1513
y = 152
yy = 183
for i in range(40):
 #enter on the group
 pyautogui.click(x=339, y=383)
 #clicking into the contact
 pyautogui.click(x=x, y=y)
 time.sleep(1)
 pyautogui.click(x=x, y=y)
 time.sleep(random.uniform(1.2, 3.5))
 pyautogui.click(x=x, y=y)
 #the box with option of start a chat appears
 pyautogui.click(x=1400, y=yy)
 time.sleep(random.uniform(0.7, 1.8))
 #click on the chat
 pyautogui.click(x=788, y=966)
 time.sleep(random.uniform(2.0, 3.8))
 pyperclip.copy(text)
 pyautogui.hotkey('ctrl','v')
 time.sleep(random.uniform(2.0, 4.0))
 pyautogui.press('enter')
 time.sleep(random.uniform(4.5, 10.3))
 y += 17
 yy += 18
 if i == 39:
  pygame.init()
  pygame.mixer.music.load("beet.mp3")
  pygame.mixer.music.play()

Im expecting to find a answer to ignore when the phone does not exist and just proceed with the looping with the contacts below (with the y += 17, yy +=18)


Solution

  • You can't do this with pyautogui library since it's just a mouse/keyboard simulation library.

    I would try to accomplish this using some libraries that include text recognition functions such as OpenCv or TesseractOCR, google them.