环境介绍:Python 3.9.1
自行下载appium和相关依赖
本代码是无聊写的,运行也正常,但是就是抢不到,抢票需要天时,地利,人和,不要用UI最好
我链接的是夜神虚拟模拟器文章来源:https://www.toymoban.com/news/detail-519122.html
特此说明:所有的UI自动化抢票以个人见解,95%往上抢不到票文章来源地址https://www.toymoban.com/news/detail-519122.html
import os
from appium import webdriver
import time
from selenium.webdriver.common.by import By
def get_size(driver):
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
return x, y
def swipe_down(driver, t):
"""
滑动时X轴不变,Y轴由小到大
"""
screensize = get_size(driver)
x1 = int(screensize[0] * 0.5) # x坐标
y1 = int(screensize[1] * 0.25) # 起始y坐标
y2 = int(screensize[1] * 0.75) # 终点y坐标
driver.swipe(x1, y1, x1, y2, t)
heard = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
desired_caps = {
"platformName": "Android",
"platformVersion": "7.1.3",
"appPackage": "cn.damai",
"appActivity": "cn.damai.search.v2.SearchActivity",
"noReset": True,
"adbExecTimeout": 200000,
"headers": heard
}
# name = "作者"
# concert_name = "【北京】DEADLINE 小鬼-作者2023巡回演唱会-北京站"
# session = '2023-06-03 周六 19:00'
# money = '看台1580元'
name = "作者"
concert_name = "武汉站"
session = '2023-06-25 周日 19:00'
money = '看台855'
try:
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps) ##启动服务器地址,后面跟的是手机信息
driver.implicitly_wait(8)
print("开始输入……")
driver.find_element(By.ID, "cn.damai:id/header_search_v2_input").send_keys(name)
driver.find_element(By.XPATH, '//android.widget.TextView[@text="{}"]'.format(name)).click()
time.sleep(0.5)
#获取演唱会场次
# try:
# print('点击场次') # 但是时间点长
# driver.find_element(By.XPATH, "//*[@text='{}']".format(concert_name)).click()
# except:
# print('点击场次')#需要手动配置
# driver.tap([(186, 280), (864, 364)], 300)
# driver.tap([(186, 280), (864, 364)], 300)
time.sleep(1)
#获取当前是否能够购买,如果能够购买直接进入
sell_status = driver.find_element(By.ID, "cn.damai:id/tv_left_main_text").text
while True:
print('现在售票状态处于:{}中....'.format(sell_status))
if sell_status.strip() == '立即预定' or sell_status.strip() == '立即购买':
driver.find_element(By.ID, "cn.damai:id/tv_left_main_text").click()
break
else:
# 点击立即购买
swipe_down(driver, 200)
# time.sleep(0.1)
sell_status = driver.find_element(By.ID, "cn.damai:id/tv_left_main_text").text
time.sleep(0.5)
#选择场次
try:
while True:
driver.find_element(By.XPATH, "//android.widget.TextView[@text='{}']".format(session)).click()
time.sleep(0.1)
driver.find_element(By.XPATH, "//android.widget.TextView[@text='{}']".format(money)).click()
#点击立即确定
driver.tap([(354, 1496), (546, 1584)], 300)
time.sleep(0.1)
try:
driver.find_element(By.ID,"cn.damai:id/damai_theme_dialog_confirm_btn")
driver.find_element(By.ID, "cn.damai:id/damai_theme_dialog_confirm_btn").click()
time.sleep(0.5)
except:
break
#处理票价
# num = 1
# while num > 1:
# driver.find_element(By.ID, "cn.damai:id/img_jia").click()
# num -= 1
# driver.tap([(450, 1496), (858, 1584)], 300)
# driver.find_element(By.XPATH, '//android.widget.TextView[@text="马茂财"]').click()
# try:
# driver.find_element(By.XPATH,'//android.widget.TextView[@text="支付宝"]').click()
# except:
# pass
#点击确定
driver.tap([(658, 1500), (858, 1580)], 300)
except:
driver.find_element(By.ID, "cn.damai:id/damai_theme_dialog_cancel_btn']").click()
driver.tap([(354, 1496), (546, 1584)], 300)
time.sleep(0.5)
# try:
# driver.find_element(By.XPATH, '//android.widget.TextView[@text="预留身份证信息"]')
# except:
#处理票价
# num = 1
# while num > 1:
# driver.find_element(By.ID, "cn.damai:id/img_jia").click()
# num -= 1
driver.tap([(450, 1496), (858, 1584)], 300)
# driver.find_element(By.XPATH, '//android.widget.TextView[@text="预留身份证信息"]').click()
# try:
# driver.find_element(By.XPATH,'//android.widget.TextView[@text="支付宝"]').click()
# except:
# pass
#点击确定
driver.tap([(658, 1500), (858, 1580)], 300)
except Exception as e:
raise Exception(e)
finally:
os.system("adb uninstall io.appium.settings")
# os.system("adb uninstall io.appium.unlock")
# driver.quit()
到了这里,关于大麦抢票-Appium实现抢票软件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!