'Will loops make this reliable, or is there a better way?
Mission : Using 3 (regions) web pages open at once, I want to be able to use pyautogui to go through a series of pages and enter info and click various buttons on the way, and then repeat the process until completed.
import pyautogui as pg
pg.FAILSAFE = True
from time import sleep
for i in range(1041, 1200, 3):
# trpl click url for region 1, enter url and press enter
sleep(1)
pg.tripleClick(x=1057, y=56)
pg.typewrite("web_address_goes_here%d" % i)
pg.press('enter')
i = i + 1
# trpl click url for region 2, enter url and press enter
pg.tripleClick(x=2734, y=56)
pg.typewrite("web_address_goes_here%d" % i)
pg.press('enter')
i = i + 1
# trpl click url for region 3, enter url and press enter
pg.tripleClick(x=4497, y=56)
pg.typewrite("web_address_goes_here%d" % i)
pg.press('enter')
sleep(4)
# click the selected button and fill in form
# region 1
if pg.locateOnScreen('click_button.png', confidence=0.8, region=(72, 148, 1606, 1236)):
x, y = pg.locateCenterOnScreen('click_button.png', confidence=0.8, region=(72, 148, 1606, 1236))
pg.click(x, y)
pg.write('message here')
print('success reg1')
sleep(1)
# region 2
if pg.locateOnScreen('click_button.png', confidence=0.8, region=(1635, 156, 3322, 1281)):
x, y = pg.locateCenterOnScreen('click_button.png', confidence=0.8, region=(1635, 156, 3322, 1281))
pg.click(x, y)
pg.write('message here')
print('success reg2')
sleep(1)
# region 3
if pg.locateOnScreen('click_button.png', confidence=0.8, region=(3561, 154, 5119, 1142)):
x, y = pg.locateCenterOnScreen('click_button.png', confidence=0.8, region=(3561, 154, 5119, 1142))
pg.click(x, y)
pg.write('message here')
print('success reg3')
sleep(1)
# change info in box and continue
# region 1
if pg.locateOnScreen('change_date.png', confidence=0.9, region=(72, 148, 1606, 1236)):
x, y = pg.locateCenterOnScreen('change_date.png', confidence=0.9, region=(72, 148, 1606, 1236))
pg.click(x, y)
sleep(0.5)
if pg.locateOnScreen('continue_button.png', confidence=0.9, region=(72, 148, 1606, 1236)):
x, y = pg.locateCenterOnScreen('continue_button.png', confidence=0.9, region=(72, 148, 1606, 1236))
pg.click(x, y)
sleep(0.5)
# region 2
if pg.locateOnScreen('change_date.png', confidence=0.9, region=(1635, 156, 3322, 1281)):
x, y = pg.locateCenterOnScreen('change_date.png', confidence=0.9, region=(1635, 156, 3322, 1281))
pg.click(x, y)
sleep(0.5)
if pg.locateOnScreen('continue_button.png', confidence=0.9, region=(1635, 156, 3322, 1281)):
x, y = pg.locateCenterOnScreen('continue_button.png', confidence=0.9, region=(1635, 156, 3322, 1281))
pg.click(x, y)
sleep(0.5)
# region 3
if pg.locateOnScreen('change_date.png', confidence=0.7, region=(3561, 154, 5119, 1142)):
x, y = pg.locateCenterOnScreen('change_date.png', confidence=0.7, region=(3561, 154, 5119, 1142))
pg.click(x, y)
sleep(0.5)
if pg.locateOnScreen('continue_button.png', confidence=0.9, region=(3561, 154, 5119, 1142)):
x, y = pg.locateCenterOnScreen('continue_button.png', confidence=0.9, region=(3561, 154, 5119, 1142))
pg.click(x, y)
sleep(2)
# press final button
# region 1
if pg.locateOnScreen('click_finish.png', confidence=0.9, region=(72, 148, 1606, 1236)):
x, y = pg.locateCenterOnScreen('click_finish.png', confidence=0.9, region=(72, 148, 1606, 1236))
pg.click(x, y)
# region 2
if pg.locateOnScreen('click_finish.png', confidence=0.9, region=(1635, 156, 3322, 1281)):
x, y = pg.locateCenterOnScreen('click_finish.png', confidence=0.9, region=(1635, 156, 3322, 1281))
pg.click(x, y)
# region 3
if pg.locateOnScreen('click_finish.png', confidence=0.9, region=(3561, 154, 5119, 1142)):
x, y = pg.locateCenterOnScreen('click_finish.png', confidence=0.9, region=(3561, 154, 5119, 1142))
pg.click(x, y)
sleep(1)
print('success for all 3')
i = i + 1
Although this does work, it's not reliable even when I add more time delay between each stage. Sometimes it doesn't recognise the button, sometimes the web page takes too long to load or sometimes doesn't load at all. So I would like to make it more reliable, maybe by adding loops so it can check for the button several times and if it doesn't find it within a number of try's, it is able to continue on with the other regions without failing. Or maybe even loading that particular page again if it failed the first time? Or maybe there is a better way? Either way, I have spent too long trying to use loops and I am currently defeated as I never get the desired outcome when I try. Any help is much appreciated!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
