使用pyautogui实现坐标定位,自动化

pyautogui是一款自动化操作库,可以根据图片定位到坐标,其原理是截取一张桌面图片,然后根据传入的图片,去查找图片在桌面上的坐标,如果按照了opencv,则会优先使用opencv,否则使用pillow。

pyautuogui的方法可以参考https://blog.csdn.net/qq350146607/article/details/99409295 这篇文章,或者鼠标点在pyautogui方法上,按ctrl+B查看源码

使用pyautogui对上篇中的User-Agent Switcher扩展插件进行自动化切换ua,

import pyautogui
import pyperclip
import random
useragents=['Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36','Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36']

ua_refresh.png

ua0.png

ua1.png

ua2.png

ua3.png

ua40.png

ua41.png

ua42.png

ua5.png

 

def left_click(path): #左键点击
    left_click1 = pyautogui.locateOnScreen(path)
    print('left_click1::', left_click1)  # 返回屏幕所在位置
    if left_click1:
        url_x, url_y = pyautogui.center(left_click1)
        pyautogui.leftClick(url_x, url_y)
        return True
    return False


def change_ua():
    ua=random.choice(useragents)
    for i in range(2):
        result0 = left_click('./images/ua{}.png'.format(i))
        if result0:
            break
    result2 = left_click('./images/ua2.png')
    result3 = left_click('./images/ua3.png')
    result4 = left_click('./images/ua40.png')
    for j in range(1,3):
        result5 = left_click('./images/ua4{}.png'.format(j))
        if result5:
            pyautogui.hotkey('ctrl','a')
            pyautogui.hotkey('backspace')
            pyperclip.copy(ua)
            pyautogui.hotkey('ctrl', 'v')
            pyautogui.press('enter')
    result6 = left_click('./images/ua_refresh.png')
    left_click('./images/ua5.png')

pyperclip 执行鼠标复制,pyautogui.hotkey执行快捷键,pyautogui的缺点是可移植性差,根据图片定位,浏览器界面尺寸稍微变化,就会定位不到

保持浏览器始终前置,并固定其位置

titlename = pyautogui.getActiveWindowTitle()
hwnd = win32gui.FindWindow(0, titlename) #获取句柄
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,0, 0, 1061, 878, win32con.SWP_SHOWWINDOW)

你可能感兴趣的:(pyppeteer)