zoukankan      html  css  js  c++  java
  • 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()
        }
    })()
  • 相关阅读:
    浏览器屏蔽百度搜索右侧热搜推荐脚本,收拾流氓头子
    js简单代码实现大banner轮播
    jquery回到顶部代码
    jquery实现隔行换色及移入移出效果
    利用:before和:after给段落添加效果的应用实例
    nginx配置文件应对网站攻击采集垃圾蜘蛛的方法总结
    Cygwin统计日志常用代码,欢迎各位大神补全
    原生js鼠标拖拽div左右滑动
    Day 82 VUE——基础
    Day 81 ES6
  • 原文地址:https://www.cnblogs.com/zyfeng/p/13650680.html
Copyright © 2011-2022 走看看