点击、输入
import asyncio
import random
from pyppeteer import launch
def input_time_random():
return random.randint(100, 151)
async def main():
browser = await launch({'headless':False})
page = await browser.newPage()
await page.goto('https://login.taobao.com')
await page.waitFor(3 * 1000)
await page.click('#J_QRCodeLogin > div.login-links > a.forget-pwd.J_Quick2Static')
await page.waitFor(3 * 1000)
await page.type('#TPL_username_1', '123123', {'delay': input_time_random() - 50})
await page.type('#TPL_password_1', '232322332', {'delay': input_time_random()})
await page.waitFor(3 * 1000)
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
点击用page.click方法,默认是css-selector
await page.click('#J_QRCodeLogin > div.login-links > a.forget-pwd.J_Quick2Static')
输入用page.type方法,第一个参数是选择器,第二个参数是要输入的字符串,第三个参数是延时设置
await page.type('#TPL_username_1', '123123', {'delay': input_time_random() - 50})
await page.type('#TPL_password_1', '232322332', {'delay': input_time_random()})
获取元素坐标
常用于获取验证码相关坐标
el = await page.querySelector('#nc_1_n1z')
box = await el.boundingBox()
await page.hover('#nc_1_n1z') #鼠标移动方块上
await page.mouse.down() #鼠标拖动操作包括按下、移动、放开
await page.mouse.move(box['x']+1000,box['y'], {'delay': random.randint(1000, 2000),'steps':3})
await page.mouse.up()
滚动
以简书首页作为例子
import asyncio
import random
from pyppeteer import launch
def input_time_random():
return random.randint(100, 151)
async def main():
browser = await launch({'headless':False})
page = await browser.newPage()
await page.goto('//www.greatytc.com')
await page.waitFor(3 * 1000)
# await page.evaluate('window.scrollBy(0, window.innerHeight)') #淘宝滚动加载用
await page.evaluate('window.scrollBy(0, document.body.scrollHeight)')
await page.waitFor(5 * 1000)
await browser.close()
asyncio.get_event_loop().run_until_complete(main())