iphone12/13抢购自动化测试脚本
本文核心内容
- iphone13 有货通知 NodeJs脚本
- iphone13 抢购的python+selenium自动化测试脚本
- iphone12 预约抢购的python+selenium自动化测试脚本
首先说明本菜狗不是什么专业评测,起因只是因为刷新闻时,看到铺天盖地的新闻:13有多香,高刷有多好,供货多紧张,根本抢不到等。作为一个好奇心重的菜狗,就只是想单纯的看看这确有其事还是故弄玄虚的噱头!
看起来是挺不错,但是价格也很感人
看似确实货源紧张,都快排到圣诞节了,而且现货也没有,只能在线购买,可能网上说的是真的?
选了一下我要取货,怎么刷都刷不出来,表面看起来确实是没货啊!
为了解决这个抢不到或者无法及时得到有货通知的问题,采用以下解决方法!
当有货时发送邮件通知,通过手速进行抢购
/*
* @Author: JavaDog
* @Date: 2021-09-19 21:22:49
* @LastEditTime: 2021-10-12 14:55:49
* @Description: iphone13 pro 银色 256g/512g
* @FilePath: apple13.js
*/
var https = require("https");
var http = require("http");
var querystring = require("querystring");
var request = require("request");
// 是否有货地址,可以替换成你的自己想要的型号
let url = [
"https://www.apple.com.cn/shop/fulfillment-messages?pl=true&mt=compact&parts.0=MLTG3CH/A&searchNearby=true&store=R557",
"https://www.apple.com.cn/shop/fulfillment-messages?pl=true&mt=compact&parts.0=MLTC3CH/A&searchNearby=true&store=R557",
];
// 循环定时器
setInterval(() => {
url.forEach((item) => {
https.get(item, function (response) {
var body = [];
response.on("data", function (chunk) {
body.push(chunk);
});
response.on("end", function () {
body = Buffer.concat(body);
let json = JSON.parse(body.toString());
let content = json.body.content;
let pickupMessage = content.pickupMessage;
let store = pickupMessage.stores[0];
let partsAvailability = store.partsAvailability;
let obj =
partsAvailability["MLTG3CH/A"] ||
partsAvailability["MLTC3CH/A"] ||
partsAvailability["MLTJ3CH/A"] ||
partsAvailability["MLTE3CH/A"];
let pickupDisplay = obj.pickupDisplay;
let storePickupProductTitle = obj.storePickupProductTitle;
let pickupSearchQuote = obj.pickupSearchQuote;
// 重要参数拼接
let result =
storePickupProductTitle +
" " +
pickupDisplay +
" " +
pickupSearchQuote +
" " +
new Date();
if (pickupDisplay != "unavailable" || pickupDisplay == "available") {
// 有货
console.log(result);
// 此处注意,请改成自己的邮件发送服务器,或者其他通知平台,本人的是自己的阿里云邮箱
mail(result, storePickupProductTitle);
} else {
// 无货
console.log(result);
}
});
});
});
}, 1000);
// 有货邮件提醒
function mail(noSimilarModelsText, storePickupProductTitle) {
var options = {
headers: {
"Content-Type": "application/json",
"admin-authorization": "dfaf86e4e45f4906a6ef8d180efba1c0",
},
url: "https://blog.javadog.net/api/admin/mails/test",
method: "POST",
json: true,
body: {
to: "[email protected]",
subject: storePickupProductTitle,
content: noSimilarModelsText,
},
};
function callback(error, response, data) {
console.log("----info------", data);
}
request(options, callback);
}
node apple.js
启动时,使用命令 node apple.js 即可,前置条件需要已安装node,如未安装,请参考 Node.js 安装配置,启动后详见下图。如需放置服务器启动,请参考pm2应用进程管理器。
经过放置服务器24小时不间断的测试得出:每天还是有少量的现货,但只在早上9-12点之间,每天现货量大约在1-5个左右(青岛地区测试),秒杀时间基本在半分钟之内,否则立刻无货
通过python+selenium自动化,让代码帮我们处理。
此处用iphone 12作为流程演示,因为12基本无需抢购,流程可以跑通
本菜狗之前没有接触过python,写的垃圾请大佬勿喷!
了解python+selenium,可可以参考 Python+Selenium基础入门及实践
如果没有chromedriver.exe 请按照谷歌浏览器版本匹配规则进行选择!本人谷歌版本94.0.4606.81 32位点击下载本人所用的 chromedriver.exe
切记chromedriver.exe依赖如果不是32位的话可能会报如下错误
WebDriverException:Message:unknown error:cannot find Chrome binary
from selenium import webdriver
from datetime import datetime
from selenium.webdriver.support.select import Select
import time
# iphone12 自动化测试
print("iphone12 自动化测试开始")
# 访问测试的url定义
url = "https://www.apple.com.cn/shop/buy-iphone/iphone-12"
# 1. 创建浏览器对象 这里的Chrome中的变量是chromedriver的驱动地址
driver = webdriver.Chrome()
# 2. 跳转到apple官网
driver.get(url)
# 3. 隐式等待 设置 防止预售的网络的阻塞
driver.implicitly_wait(10)
# 4.1 开始选择规格【此处我选择了-12 mini】
element_sku = driver.find_element_by_name('dimensionScreensize')
driver.implicitly_wait(10)
element_sku.click()
# 4.2 选择颜色【此处我选择了-白色】
element_color = driver.find_element_by_xpath(
'//*[@value="white"]')
driver.execute_script("arguments[0].click();", element_color)
driver.implicitly_wait(10)
# 4.3 选择内存【此处我选择了-256g】
element_memory = driver.find_element_by_xpath(
'//*[@value="256gb"]')
driver.execute_script("arguments[0].click();", element_memory)
driver.implicitly_wait(10)
# 4.4 你是否有智能手机要折抵 【此处我选择了-没有旧机折扣】
element_old = driver.find_element_by_xpath('//*[@id="noTradeIn"]')
driver.execute_script("arguments[0].click();", element_old)
driver.implicitly_wait(10)
# 4.5 Applecare 【此处我选择了-无Applecare】
element_care = driver.find_element_by_id('applecareplus_58_noapplecare_label')
driver.execute_script("arguments[0].click();", element_care)
driver.implicitly_wait(10)
# 4.6 添加到购物袋
element_car = driver.find_element_by_xpath(
'//*[@value="add-to-cart"]')
driver.execute_script("arguments[0].click();", element_car)
driver.implicitly_wait(10)
# 5 页面跳转查看购物袋
element_check = driver.find_element_by_xpath(
'//*[@value="proceed"]')
driver.execute_script("arguments[0].click();", element_check)
driver.implicitly_wait(10)
# 6 结账
element_check_out = driver.find_element_by_xpath(
'//*[@id="shoppingCart.actions.navCheckout"]')
driver.execute_script("arguments[0].click();", element_check_out)
driver.implicitly_wait(10)
# 7.1 输入用户名
element_username = driver.find_element_by_id(
'signIn.customerLogin.appleId')
element_username.send_keys('appleId账号')
driver.implicitly_wait(10)
# 7.2 输入密码
element_password = driver.find_element_by_id(
'signIn.customerLogin.password')
element_password.send_keys('密码')
driver.implicitly_wait(10)
# 7.3 点击登录
element_login = driver.find_element_by_id(
'signin-submit-button')
element_login.click()
driver.implicitly_wait(10)
# 8.1 你希望如何收到订单商品 【此处我选择了-我要取货】
element_want_order = driver.find_element_by_id(
'fulfillmentOptionButtonGroup1')
driver.execute_script("arguments[0].click();", element_want_order)
driver.implicitly_wait(10)
# 8.2 点击显示此地附近的零售店
element_selectdistrict = driver.find_element_by_xpath(
'//*[@aria-describedby="rs-fulfillment-storelocator-error"]')
driver.execute_script("arguments[0].click();", element_selectdistrict)
driver.implicitly_wait(10)
# 8.3 点击山东
element_provice = driver.find_element_by_xpath(
'//*[@value="山东"]')
driver.execute_script("arguments[0].click();", element_provice)
driver.implicitly_wait(10)
# 8.4 点击青岛
element_city = driver.find_element_by_xpath(
'//*[@value="青岛"]')
driver.execute_script("arguments[0].click();", element_city)
driver.implicitly_wait(10)
# 8.5 点击市南
element_area = driver.find_element_by_xpath(
'//*[@value="市南区"]')
driver.execute_script("arguments[0].click();", element_area)
driver.implicitly_wait(20)
# 8.6 选择取货零售店 【此处我选择了-Apple 青岛万象城】
element_pickupTab = driver.find_element_by_id(
'checkout.fulfillment.pickupTab.pickup.storeLocator-R557')
driver.execute_script("arguments[0].click();", element_pickupTab)
driver.implicitly_wait(20)
# 8.7 选择取货时间 【根据时间自己定】
element_pickup_time = driver.find_element_by_xpath(
'//*[@value="11"]')
driver.execute_script("arguments[0].click();", element_pickup_time)
driver.implicitly_wait(10)
# 8.8 选择取货时间段 【此处我选择了-默认第一个时间段】
element_time_quantum = driver.find_element_by_xpath(
'//*[@aria-labelledby="timeWindows_label"]')
Select(element_time_quantum).select_by_index(1)
driver.implicitly_wait(15)
# 8.9 继续填写取货详情
element_checkout = driver.find_element_by_id(
'rs-checkout-continue-button-bottom')
driver.implicitly_wait(15)
driver.execute_script("arguments[0].click();", element_checkout)
element_checkout.click()
driver.implicitly_wait(10)
# 9.1 请填写收件人手机号码
element_Phone = driver.find_element_by_name('fullDaytimePhone')
element_Phone.send_keys('手机号')
driver.implicitly_wait(10)
# 9.2 请填写收件人身份证
element_nationalId = driver.find_element_by_name('nationalId')
element_nationalId.send_keys('身份证')
driver.implicitly_wait(10)
# 9.3 检查订单
element_checkoutPay = driver.find_element_by_id(
'rs-checkout-continue-button-bottom')
driver.execute_script("arguments[0].click();", element_checkoutPay)
driver.implicitly_wait(10)
# 10 立即下单 【此处我选择了-微信支付】
element_billingOptions = driver.find_element_by_id(
'checkout.billing.billingOptions.options.2')
driver.execute_script("arguments[0].click();", element_billingOptions)
driver.implicitly_wait(10)
# 11.1 确定
element_orderPay = driver.find_element_by_id(
'rs-checkout-continue-button-bottom')
driver.execute_script("arguments[0].click();", element_orderPay)
driver.implicitly_wait(15)
# 12 确认订单
element_endPay = driver.find_element_by_xpath(
'//*[@aria-describedby="rs-checkout-continuedisclaimer-bottom"]')
driver.execute_script("arguments[0].click();", element_endPay)
driver.implicitly_wait(15)
# 11 退出浏览器
time.sleep(10)
# driver.quit()
print("iphone12 自动化测试结束")
python apple-12.py
其中账号密码相关需要替换成自己的,并且苹果官网会有卡顿延时,及403等异常,属于正常情况。
因iphone13 无货,代码中加入了一个while判断,重复拉取省市区进行有无货的重新加载
from selenium import webdriver
from datetime import datetime
from selenium.webdriver.support.select import Select
import pdb
import time
# iphone13 自动化测试
print("iphone13 自动化测试开始")
# 访问测试的url定义
url = "https://www.apple.com.cn/shop/buy-iphone/iphone-13-pro"
# 1. 创建浏览器对象 这里的Chrome中的变量是chromedriver的驱动地址
driver = webdriver.Chrome()
# 2. 跳转到apple官网
driver.get(url)
# 3. 隐式等待 设置 防止预售的网络的阻塞
driver.implicitly_wait(10)
# 4. 开始选择规格【此处我选择了-13 pro】
element_sku = driver.find_element_by_name('dimensionScreensize')
driver.implicitly_wait(10)
element_sku.click()
# 4.2 选择颜色【此处我选择了-银色】
element_color = driver.find_element_by_xpath(
'//*[@value="silver"]')
driver.execute_script("arguments[0].click();", element_color)
driver.implicitly_wait(10)
# 4.3 选择内存【此处我选择了-256g】
element_memory = driver.find_element_by_xpath(
'//*[@value="256gb"]')
driver.execute_script("arguments[0].click();", element_memory)
driver.implicitly_wait(10)
# 4.4 你是否有智能手机要折抵 【此处我选择了-没有旧机折扣】
element_old = driver.find_element_by_xpath('//*[@id="noTradeIn"]')
driver.execute_script("arguments[0].click();", element_old)
driver.implicitly_wait(10)
# 4.5 Applecare 【此处我选择了-无Applecare】
element_care = driver.find_element_by_id('iphonexs_ac_iup_noapplecare_label')
driver.execute_script("arguments[0].click();", element_care)
driver.implicitly_wait(10)
# 4.6 添加到购物袋
element_car = driver.find_element_by_xpath(
'//*[@value="add-to-cart"]')
driver.execute_script("arguments[0].click();", element_car)
driver.implicitly_wait(10)
# 5 页面跳转查看购物袋
element_check = driver.find_element_by_xpath(
'//*[@id="root"]/div[2]/div/div/div[2]/div/form/button')
driver.execute_script("arguments[0].click();", element_check)
driver.implicitly_wait(10)
# 6 结账
element_check_out = driver.find_element_by_xpath(
'//*[@id="shoppingCart.actions.checkout"]')
driver.execute_script("arguments[0].click();", element_check_out)
driver.implicitly_wait(10)
# 7.1 输入用户名
element_username = driver.find_element_by_id(
'signIn.customerLogin.appleId')
element_username.send_keys('苹果appleId')
driver.implicitly_wait(10)
# 7.2 输入密码
element_password = driver.find_element_by_id(
'signIn.customerLogin.password')
element_password.send_keys('密码')
driver.implicitly_wait(10)
# 7.3 点击登录
element_login = driver.find_element_by_id(
'signin-submit-button')
element_login.click()
driver.implicitly_wait(10)
# 8.1 你希望如何收到订单商品 【此处我选择了-我要取货】
element_want_order = driver.find_element_by_id(
'fulfillmentOptionButtonGroup1')
driver.execute_script("arguments[0].click();", element_want_order)
driver.implicitly_wait(10)
# 8.2 点击显示此地附近的零售店
selectdistrict = driver.find_element_by_xpath(
'//*[@aria-describedby="rs-fulfillment-storelocator-error"]')
driver.execute_script("arguments[0].click();", selectdistrict)
driver.implicitly_wait(10)
# 8.3 点击山东
element_provice = driver.find_element_by_xpath(
'//*[@value="山东"]')
driver.execute_script("arguments[0].click();", element_provice)
driver.implicitly_wait(10)
# 8.4 点击青岛
element_city = driver.find_element_by_xpath(
'//*[@value="青岛"]')
driver.execute_script("arguments[0].click();", element_city)
driver.implicitly_wait(10)
# 8.5 点击市南
element_area = driver.find_element_by_xpath(
'//*[@value="市南区"]')
driver.execute_script("arguments[0].click();", element_area)
driver.implicitly_wait(20)
# 因为无货需要判断元素是否可以点击
isOK = driver.find_element_by_id(
'checkout.fulfillment.pickupTab.pickup.storeLocator-R557').is_enabled()
isOKFlag = bool(1 - isOK)
#print("准备isOKFlag " + str(isOKFlag))
# while循环查看是否有货
while isOKFlag:
try:
# 重新调用省市区
#print("进来了isOKFlag " + str(isOKFlag))
driver.implicitly_wait(20)
selectdistrict = driver.find_element_by_xpath(
'//*[@aria-describedby="rs-fulfillment-storelocator-error"]')
driver.execute_script("arguments[0].click();", selectdistrict)
driver.implicitly_wait(20)
provice = driver.find_element_by_xpath(
'//*[@value="山东"]')
driver.execute_script("arguments[0].click();", provice)
city = driver.find_element_by_xpath(
'//*[@value="青岛"]')
driver.execute_script("arguments[0].click();", city)
area = driver.find_element_by_xpath(
'//*[@value="市南区"]')
driver.execute_script("arguments[0].click();", area)
driver.implicitly_wait(20)
isOK = driver.find_element_by_id(
'checkout.fulfillment.pickupTab.pickup.storeLocator-R557').is_enabled()
isOKFlag = bool(1 - isOK)
#print("最后了isOK " + str(isOKFlag))
except:
print("异常了 ")
break
# tips: 经验证,苹果官网如果在付款页面之前实体店无货,若有货后在结算页面也无法选择实体店
# 8.6 选择取货零售店 【此处我选择了-Apple 青岛万象城】
element_pickupTab = driver.find_element_by_id(
'checkout.fulfillment.pickupTab.pickup.storeLocator-R557')
driver.execute_script("arguments[0].click();", element_pickupTab)
driver.implicitly_wait(20)
# 8.7 选择取货时间 【根据时间自己定】
element_pickup_time = driver.find_element_by_xpath(
'//*[@value="11"]')
driver.execute_script("arguments[0].click();", element_pickup_time)
driver.implicitly_wait(10)
# 8.8 选择取货时间段 【此处我选择了-默认第一个时间段】
element_time_quantum = driver.find_element_by_xpath(
'//*[@aria-labelledby="timeWindows_label"]')
Select(element_time_quantum).select_by_index(1)
driver.implicitly_wait(15)
# 8.9 继续填写取货详情
element_checkout = driver.find_element_by_id(
'rs-checkout-continue-button-bottom')
driver.implicitly_wait(15)
driver.execute_script("arguments[0].click();", element_checkout)
element_checkout.click()
driver.implicitly_wait(20)
# 9.1 请填写收件人手机号码
element_Phone = driver.find_element_by_name('fullDaytimePhone')
element_Phone.send_keys('电话')
driver.implicitly_wait(10)
# 9.2 请填写收件人身份证
element_nationalId = driver.find_element_by_name('nationalId')
element_nationalId.send_keys('身份证号')
driver.implicitly_wait(10)
# 9.3 检查订单
element_checkoutPay = driver.find_element_by_id(
'rs-checkout-continue-button-bottom')
driver.execute_script("arguments[0].click();", element_checkoutPay)
driver.implicitly_wait(10)
# 10 立即下单 【此处我选择了-微信支付】
element_billingOptions = driver.find_element_by_id(
'checkout.billing.billingOptions.options.2')
driver.execute_script("arguments[0].click();", element_billingOptions)
driver.implicitly_wait(10)
# 11.1 确定
element_orderPay = driver.find_element_by_id(
'rs-checkout-continue-button-bottom')
driver.execute_script("arguments[0].click();", element_orderPay)
driver.implicitly_wait(15)
# 12 确认订单
element_endPay = driver.find_element_by_xpath(
'//*[@aria-describedby="rs-checkout-continuedisclaimer-bottom"]')
driver.execute_script("arguments[0].click();", element_endPay)
driver.implicitly_wait(15)
# 11 退出浏览器
time.sleep(10)
# driver.quit()
print("iphone13 自动化测试结束")
python apple-13.py
以上订单均为测试所用,本菜狗并没有购买iphone,也希望大家还是多多支持国货,没有针对,没有道德绑架,只是希望我们国家产物越来越优秀❤️
我是JavaDog,谢谢博友耐心看完, 抽空来我狗窝瞅瞅呗 blog.javadog.net