page.evaluate(),查看puppeteer 的api , 经过api 咱们能够大概了解,pgeFunction传入的是一个页面执行方法。返回一个pageFunction的执行结果。chrome
1. 咱们今天作个案例,好比再同城上搜索某个时间段的机票,可是咱们发现,时间空间几乎很难定位到,出发城市,到达城市能够定位到而且能够用page.type()方法输入地址,可是时间呢,怎么定位呢,咱们分析一下:api
1.时间元素,咱们能够定位到把 :element = '#txtAirplaneTime1'less
2.再分析,咱们发现选中某个时间后,输入框是“2019-08-20”async
3.那好了,咱们是否是直接运用js 把值传进入就好了,也许有人问为何page.type()不行,首先这个空间不是文本输入,因此没法使用type ui
4.打开控制台调试下js代码 document.querySelector("#txtAirplaneCity1").value = “” (自行调试,不演示了)lua
5.完整代码spa
const puppeteer = require('puppeteer'); (async () => { const brower = await puppeteer.launch({ executablePath:'D:\\wangxiao\\chrome-win\\chrome-win\\chrome.exe', headless:false, ignoreDefaultArgs:["--enable-automation"], defaultViewport:{width:1200,height:700} }); const page = await brower.newPage(); await page.goto('https://www.ly.com',{waitUntil:"networkidle2"}); await page.waitFor("#txtAirplaneCity1"); await page.waitFor("#txtAirplaneCity2"); await page.evaluate(() => { document.querySelector("#txtAirplaneCity1").value = "上海"; document.querySelector("#txtAirplaneCity2").value = "成都"; document.querySelector("#txtAirplaneTime1").value = "2019-08-20"; }) const btn = await page.waitForSelector('#airplaneSubmit'); await btn.click(); })().catch(error =>{console.log('error')});