ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

puppeteer 模拟登录淘宝

2020-09-11 11:32:11  阅读:811  来源: 互联网

标签:登录 await puppeteer slidePosition let 淘宝 password mouse page


(async() => {
    // 模拟登录
    async function login(page){
        console.log('正在登陆....')
        await page.goto('https://login.1688.com/member/signin.htm', {
            waitUntil: 'networkidle2',   // 等待网络空闲,在跳转加载页面
            waitUntil: 'domcontentloaded'
        })
        await page.waitForSelector("#loginchina > iframe")
        // 找到iframe
        const frame = (await page.frames())[1];
        // 跳到iframe页面去
        await page.goto(frame.url())
        // 输入账号密码
        await page.waitForSelector("#fm-login-id")
        await page.type('#fm-login-id', account, { delay: 10 })
        await page.waitFor(1000)
        await page.waitForSelector("#fm-login-password")
        await page.type('#fm-login-password', pwd, { delay: 10 })
        // 验证是否有滑块
        if (await page.$('#nocaptcha-password #nc_1_n1z')) {
            // 获取滑块位置
            let slidePosition = await getRect(page, "#nc_1_n1z")
            // 滑块可滑动区域
            let blockPosition = await getRect(page, "#nc_1__scale_text")
            // 鼠标初始位置
            let initialX = slidePosition.x + slidePosition.width / 2
            let initialY = slidePosition.y + slidePosition.height / 2
            let xlength  = blockPosition.width - slidePosition.width * 0.75
            // 开始移动滑块
            for(let i = 0; i < 4; i++){
                // await page.waitFor(1500)
                await move(page, initialX, initialY, xlength)
                await page.waitFor(1500)
                let errEl = await page.$("#nocaptcha-password .errloading")
                if(errEl){
                    // 出错重置
                    await page.click("#nocaptcha-password .errloading a")
                    await page.waitForSelector("#nc_1_n1z")
                }else{
                    break
                }
            }
        }
        await page.click('.fm-btn button')
        await page.waitForSelector(".company-name")
        console.log("登陆成功")
    }

    // 获取元素位置
    async function getRect(page, selector) {
        return await page.$eval(selector, el => {
            let res = el.getBoundingClientRect()
            return {
              x: res.x,
              y: res.y,
              width: res.width,
              height: res.height
            }
        })
    }
    // 将鼠标移到某处
    async function move(page, initialX, initialY, xlength = 0, ylength = 0) {
        const mouse = page.mouse
        await mouse.move(initialX, initialY)
        await mouse.down()
        await mouse.move(initialX + xlength, initialY + ylength, { steps: 20 })
        await page.waitFor(3000)
        await mouse.up()
    }
})()

 

标签:登录,await,puppeteer,slidePosition,let,淘宝,password,mouse,page
来源: https://www.cnblogs.com/zyfeng/p/13650680.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有