• puppeteer 模拟登录淘宝


    (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,
                   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()
        }
    })()
  • 相关阅读:
    学习《Beginning iPhone 4 Development 》第6章关于View Switcher这个例子的问题
    Task、Thread、Process、Program
    Xcode Build版本号自增
    NSAutoreleasePool
    xml文件操作
    一些收集整理的JS
    网页常用小技巧
    40种网站设计常用技巧
    在虚拟主机中用ASP.NET1.1服务器端TIMER定时读取RSS信息到数据库
    删除服务器的文件夹后,session就丢失的问题
  • 原文地址:https://www.cnblogs.com/zyfeng/p/13650680.html
Copyright © 2020-2023  润新知