简单的了解了下,这个库就是PythonGUI自动化工具库,可以操作鼠标和键盘完成一系列动作,发现做测试的时候,不仅可以使用他来做功能测试,还可以做UI测试。 

import pyautogui
import time

time.sleep(3)

# 立即购买
def buy(img):
    while True:
        # 查找有没有立即购买的按钮
        buyBtn = pyautogui.locateOnScreen(img)
        print(buyBtn)
        if buyBtn is not None:
            pyautogui.click(buyBtn.left + buyBtn.width/2, buyBtn.top+buyBtn.height/2)
            break
        time.sleep(0.001)

# 选择价格
def price(img):
    while True:
        price = pyautogui.locateOnScreen(img)
        print(price)
        if price is not None:
            pyautogui.click(price.left + price.width/2, price.top+price.height/2)
            break
        time.sleep(0.001)

# 选择购买票数,一个不用这个函数,两票用一次,三票用两次
def add(img):
    addBtn = pyautogui.locateOnScreen(img)
    if addBtn is not None:
        # 点击一下,买两张
        pyautogui.click(addBtn.left + addBtn.width/2, addBtn.top+addBtn.height/2)

# 下单
def enter(img):
    enterBtn = pyautogui.locateOnScreen(img)
    if enterBtn is not None:
        pyautogui.click(enterBtn.left + enterBtn.width/2, enterBtn.top+enterBtn.height/2)

# 选择人员
def check(img):
    while True:
        checkBtn = pyautogui.locateOnScreen(img)
        print(checkBtn)
        if checkBtn is not None:
            pyautogui.click(checkBtn.left + checkBtn.width/2, checkBtn.top+checkBtn.height/2)
            break
        time.sleep(0.001)

# 添加其他人,不需要循环查找,节省时间
def checkOther(img):
    checkOtherBtn = pyautogui.locateOnScreen(img)
    print(checkOtherBtn)
    if checkOtherBtn is not None:
        pyautogui.click(checkOtherBtn.left + checkOtherBtn.width/2, checkOtherBtn.top+checkOtherBtn.height/2)

# 提交订单
def submit(img):
    submitBtn = pyautogui.locateOnScreen(img)
    print(submitBtn)
    if submitBtn is not None:
        pyautogui.click(submitBtn.left + submitBtn.width/2, submitBtn.top+submitBtn.height/2)


buy('./img/damai1.png')
price('./img/780.png')
add('./img/add.png')
enter('./img/enter.png')
check('./img/check.png')
checkOther('./img/check1.png')
submit('./img/submit.png')
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.

以上代码的图片为截图,比如大麦网购买截图啥的,举个例子,在购买的页面,有一个按钮叫做立即购买,所以截图这个区域即可,780是价格的截图,add是增加票数的截图,enter是购买的截图,check和check1是购买人的截图,submit是提交订单的截图。