标签:reload web idt 也有 python until http bre coding
企查查登陆验证使用的阿里系的滑动验证码,主要检查的是 window.navigator.webdriver
内置属性、鼠标在页面中的事件、浏览器可见性中的宽高(canvas绘制)、滑动过程中坐标轴的变化等信息。
使用puppeteer能很好的模拟页面中的事件,但是在进行测试企查查登陆时,需要浏览器的可见性,而且与分辨率也有一定的关系,需要设置浏览器和系统的分辨率为100%,否则会有问题,比如点击滑块时,会点击下面的标示性文字。
from pyppeteer import launch
import asyncio
from random import random
import time
# pip install pyppeteer
# 修改 \Lib\site-packages\pyppeteer\launcher.py
# 搜索 ‘--enable-automation‘ 将其注释
async def main():
config = {
"headless": False,
# 自己下载的文件的位置
"executablePath": "chrome-win32\\chrome.exe",
"userDataDir": "qcc_login_py",
"args": [
# ‘--disable-dev-shm-usage‘, ‘--no-sandbox‘, ‘--disable-setuid-sandbox‘,
‘--start-maximized‘,
],
"slowMo": 4
}
username = "账号"
password = "密码"
browser = await launch(config)
page = await browser.newPage()
await page.goto(‘https://www.qcc.com/user_login‘)
await (await page.J("#normalLogin")).click()
await page.type(‘#nameNormal‘, username)
await page.type(‘#pwdNormal‘, password)
while True:
# 获取滑块
# 计算滑块距离
rect = await page.evaluate("""() => {
let el = document.getElementById("nc_1_n1z")
if (el === null){
el = document.getElementById("nc_2_n1z")
}
const site = el.getBoundingClientRect();
return {left: site.left, top: site.top}
}""")
print(rect)
# 随机滑动的距离
rect[‘left‘] = rect[‘left‘] + 10 + random() * 20
rect[‘top‘] = rect[‘top‘] + 10 + random() * 20
mouse = page.mouse
await mouse.move(rect[‘left‘], rect[‘top‘])
# 移动事件
await page.touchscreen.tap(rect[‘left‘], rect[‘top‘])
await mouse.down()
await mouse.move(rect[‘left‘] + 800, rect[‘top‘], {"steps": int(random() * 400)})
await page.touchscreen.tap(rect[‘left‘]+ 800, rect[‘top‘])
await mouse.up()
# 硬性等待 内置的等待方法有问题
page.waitFor(2000)
# 检查是否通过
e = await page.J(".nc-lang-cnt")
textContent = await page.evaluate("(e) => e.textContent", e)
print("textContent = " + textContent)
if textContent == "验证通过":
break
# 重新加载
await page.reload()
await page.type(‘#nameNormal‘, username)
await page.type(‘#pwdNormal‘, password)
cookies = await page.cookies()
cookiesStr = ";".join(["{0[name]}={0[value]}".format(c) for c in cookies])
print(cookiesStr)
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
var puppeteer = require(‘puppeteer‘)
var fs = require("fs")
// 企查查登陆
const conf = {
headless: false,
// 自己下载的文件的位置
executablePath: "chrome-win32\\chrome.exe",
defaultViewport: {
width: 1920,
height: 1080
},
args: [
‘--disable-dev-shm-usage‘, ‘--no-sandbox‘, ‘--disable-setuid-sandbox‘,
‘--start-maximized‘
],
ignoreDefaultArgs: [
‘--enable-automation‘, // 关闭 window.navigator.webdriver 检查
‘--disable-extensions‘
],
slowMo: 4
}
const username = "账号";
const password = "密码";
puppeteer.launch(conf).then(async browser => {
var page = await browser.newPage()
await page.goto(‘https://www.qcc.com/user_login‘)
await (await page.$("#normalLogin")).click()
await page.type(‘#nameNormal‘, username)
await page.type(‘#pwdNormal‘, password)
while(true){
// 获取滑块
var slide_btn = await page.$(‘#nc_1_n1z‘)
// 计算滑块距离
const rect = await page.evaluate((slide_btn) => {
const {top, left, bottom, right} = slide_btn.getBoundingClientRect();
return {top, left, bottom, right}
}, slide_btn)
console.log(rect)
// 随机滑动的距离
rect.left = rect.left + 10 + Math.random() * 20
rect.top = rect.top + 10 + Math.random() * 20
const mouse = page.mouse
await mouse.move(rect.left, rect.top)
// 移动事件
await page.touchscreen.tap(rect.left, rect.top)
await mouse.down()
await mouse.move(rect.left + 800, rect.top, {steps: Math.random() * 400})
await page.touchscreen.tap(rect.left + 800, rect.top)
await mouse.up()
// 硬性等待 内置的等待方法有问题
page.waitFor(2000)
// 检查是否通过
const textContent = await page.$eval(".nc-lang-cnt", el => el.innerText)
console.log("textContent = " + textContent)
if (textContent == "验证通过"){
break
}
// 重新加载
await page.reload()
await page.type(‘#nameNormal‘, username)
await page.type(‘#pwdNormal‘, password)
}
// 登陆
await (await page.$(".login-btn")).click()
// 获取 cookie
const cookies = await page.cookies()
const cookiesStr = cookies.map(cookie => `${cookie.name}=${cookie.value}`).join(";");
console.log(cookiesStr)
fs.writeFile("cookie", cookiesStr, {encoding: "utf-8"},function (err) {
if (err){
return console.error(err)
}
})
//await page.close()
await browser.close()
})
标签:reload web idt 也有 python until http bre coding
原文地址:https://www.cnblogs.com/iFanLiwei/p/12791227.html