V
V
Vlad Efimov2020-09-05 21:36:46
Python
Vlad Efimov, 2020-09-05 21:36:46

Visual python bot?

Hello! I have a code that looks for an image and clicks on it. How to make the script wait for it to appear? And how to set the time during which it must wait, and if during this time it did not see the pixel or the picture that I set, then do another action, that is, protection from stagnation.

import pyautogui 
import cv2
import time

start = pyautogui.locateOnScreen('start.png', confidence=0.4)
pyautogui.click(start)

time.sleep(2)

startx2 = pyautogui.locateOnScreen('startx2.png', confidence=0.6)
pyautogui.click(startx2)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
David Armyan, 2020-09-06
@proboltal

As an option, you need to do a function for checking the appearance of an image like this:

def check_and_click():
  try:	
    start = pyautogui.locateOnScreen('start.png', confidence=0.4)
    return True
  except:
    print('Нет картинки, либо другая ошибка')
    return False

def check_and_click2():
  try:	
    startx2 = pyautogui.locateOnScreen('startx2.png', confidence=0.6)
    return True
  except:
    print('Нет картинки, либо другая ошибка')
    return False





#Тут вызовем функцию в цикле:

while True:
  it = check_and_click()
  if it:
    pyautogui.click(start)
    print("Good!")
    time.sleep(1)
  else:
    print("Ждем 5 секунд")
    time.sleep(5)

  it2 = check_and_click2()
  if it2:
    pyautogui.click(startx2)
    print("Good!")
    time.sleep(1)
  else:
    print("Ждем 5 секунд")
    time.sleep(5)

I did not quite understand the logic of your program, but if anything, explain

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question