1-1 click-type
var btn = await page.$("#btn") //找到某个元素
await btn.click() //点击事件
await btn.type() //用户输入事件
const puppeteer = require("puppeteer");
const url = "https://www.baidu.com";
(async ()=>{
const browser = await puppeteer.launch({
headless:false, //模拟浏览器行为
defaultViewport:null, //可视区域最大化
args: ['--start-maximized'] //浏览器窗口最大化
});
const page = await browser.newPage();
await page.goto(url)
var input = await page.$("#kw");
await input.type("hello world")
var btn = await page.$("#su")
await btn.click()
})();
var content = await page.$eval('#su',ele=>ele.value);
# $eval()获取元素的内容
1-2 模拟键盘回车事件
await page.keyboard.press('Enter');
1-3 处理多个元素
waitForSelector() //等待选择器加载
$$eval() //处理多个元素的内容,获取的是数组
const puppeteer = require("puppeteer");
const url = "http://www.kuwo.cn/search/list?key=你";
(async () => {
const browser = await puppeteer.launch({
defaultViewport: null, //可视区域最大化
});
const page = await browser.newPage();
await page.goto(url)
var input = await page.$(".search");
await input.type("大闹天宫");
await page.keyboard.press('Enter');
await page.waitForSelector(".search_list>.song_item")
const list = await page.$$eval(".search_list>.song_item",ele=>{
return ele.length;
})
console.log(list)
})()