自己编写 小米手机自动购买脚本 (仅供个人学习 不要商业使用)
作者:互联网
自己尝试用无头浏览器编写购买脚本 效果如下:
后续可以深度开发使用
const puppeteer = require('puppeteer'); const config = { user: '自己用户名', password: '自己密码', } function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } (async () => { const browser = await puppeteer.launch({ headless: false, defaultViewport: { width: 1920, height: 1080 } }); const page = await browser.newPage(); // Login await page.goto('https://account.xiaomi.com/',{ waitUntil:'domcontentloaded' }); await page.waitForSelector('input[name="account"]') await page.type('input[name="account"]', config.user.toString()); await page.type('input[name="password"]', config.password); await page.click('button[type = "submit"]') const page2 = await browser.newPage(); await page2.goto('https://www.mi.com/buy/detail?product_id=12511', { waitUntil: 'domcontentloaded' }); await page2.waitForSelector('.sale-btn') await page2.click('.sale-btn') // await page.waitFor(1000); // await page2.waitForSelector('.sale-btn') // await page2.click('.sale-btn') /* await page2.waitForSelector('.btn-primary') await page2.click('.btn-primary') */ const page3 = await browser.newPage(); await page3.goto('https://www.mi.com/buy/cart', { waitUntil: 'domcontentloaded' }); await page3.waitForSelector('.btn-primary') await page3.click('.btn-primary') await page3.waitForSelector('.el-dialog__footer .btn-primary') await page3.click('.el-dialog__footer .btn-primary') await page3.waitForSelector('.btn-primary') await page3.click('.btn-primary') await page3.waitForSelector('.total-price .btn-primary') await page3.click('.total-price .btn-primary') const page4 = await browser.newPage(); await page4.goto('https://www.mi.com/buy/checkout', { waitUntil: 'domcontentloaded' }); await page4.waitForSelector('.address-item') await page4.click('.address-item') await page4.waitForSelector('.address-item') await page4.click('.address-item') await page4.waitForSelector('.operating-button a') await page4.click('.operating-button a') })();
标签:waitForSelector,await,primary,page3,仅供,编写,btn,小米,click 来源: https://www.cnblogs.com/byksj/p/14504972.html