1.當咱們須要獲取元素內的值,與實際值作對比的時候,驗證頁面正確性;html
2.當咱們獲取某個元素內的文本值,須要拿這個值圖填補另外一個輸入框的時候;chrome
<a href="#" onclick="register();return false">註冊</a>
看一下這段html 代碼,我如今想獲得【註冊】,怎麼辦呢?咱們能夠參考puppeteer 給個人api 依葫蘆畫瓢 ,使用el => el.innerHTML 獲取內部html 值api
const puppeteer = require('puppeteer'); (async () => { const brower = await puppeteer.launch({ executablePath:'D:\\wangxiao\\chrome-win\\chrome-win\\chrome.exe', headless:false }); const page = await brower.newPage(); await page.goto('https://www.cnblogs.com/'); let res = await page.$eval('#span_userinfo a:nth-child(2)',el => el.innerHTML); console.log(res); //await brower.close(); })().catch(error =>{console.log('error')});
使用page.$$eval(selector, pageFunction[, ...args]) 獲取返回是數組,我如今想獲取每一個標題,發現他的class的父級別是同樣的,全部元素能夠寫成‘.post_nav_block li a’數組
const puppeteer = require('puppeteer'); (async () => { const brower = await puppeteer.launch({ executablePath:'D:\\wangxiao\\chrome-win\\chrome-win\\chrome.exe', headless:false }); const page = await brower.newPage(); await page.goto('https://www.cnblogs.com/'); let res = await page.$$eval('.post_nav_block li a',el => el.map(el => el.innerHTML)); console.log(res); await brower.close(); })().catch(error =>{console.log('error')});