1、
2、
// Puppeteer elements of the basic operation - the input text element and click const = Puppeteer the require ( 'Puppeteer' ) async function fun() { const browser = await puppeteer.launch({ headless: false, defaultViewport: { width: 1366, height: 768 } }) const page = await browser.newPage() await page.goto('https://www.baidu.com') input_area const = the await Page. $ ( '# kW') // positioned box enter the await input_area.type ( 'Hello World') // input text // const = search_btn Page. $ ( '# Su') // positioning 'Baidu, "the search button // the await search_btn.click () // Click the await page.click (' # su ' ) } fun()
3、
// Puppeteer text element acquired the value const = Puppeteer the require ( 'Puppeteer' ) async function fun() { const browser = await puppeteer.launch({ headless: false, defaultViewport: { width: 1366, height: 768 } }) const page = await browser.newPage() await page.goto('https://www.baidu.com') input_area const = the await Page. $ ( '# kW') // positioned box enter the await input_area.type ( 'Hello World') // input text // const = search_btn Page. $ ( '# Su') // positioning 'Baidu, "the search button // the await search_btn.click () // Click the await page.click (' # su ' ) await page.waitForSelector('div#content_left > div.result-op.c-container.xpath-log') let resultText = await page.$eval('div#content_left > div.result-op.c-container.xpath-log', ele => ele.innerHTML) console.log(`resultText=${resultText}`) } fun()
4、
// Puppeteer plurality of processing elements const = Puppeteer the require ( 'Puppeteer' ) async function fun() { const browser = await puppeteer.launch({ headless: false, defaultViewport: { width: 1366, height: 768 } }) const page = await browser.newPage() await page.goto('https://www.jd.com') INPUT const = await Page. $ ( '# Key') // positioning input box await input.type ( 'phone') // input text await page.keyboard.press ( 'the Enter') // press Enter await page.waitForSelector ( 'ul.gl-Warp> Li' ) let resultTextList = await page.$$eval('ul.gl-warp > li', eles => eles.map(ele.innerText)) console.log('resultTextList = ', resultTextList) } fun()
5、
// pupputeer switch housing off the iframe login operation const = Puppeteer the require ( 'Puppeteer' ) async function fun() { const browser = await puppeteer.launch({ headless: false, defaultViewport: { width: 1366, height: 768 } }) const page = await browser.newPage() await page.goto('https://login.anjuke.com/login/form') // print the page frame address of all the await page.frames (). Map (frame => {the console.log (frame.url ())}) // 通过frame的url定位到frame const targetFrameUrl = 'https://login.anjuke.com/login/iframeform' const frame = await page.frames().find(frame => frame.url().includes(targetFrameUrl)) const phone = await frame.waitForSelector('#phoneIpt') await phone.type('13530125464') } fun()
6、
// Puppeteer drag operation codes aliyun the async function Fun () { const browser = await puppeteer.launch({ headless: false, defaultViewport: { width: 1366, height: 768 }, ingoreDefaultArgs: ['--enable-automation'] }) const page = await browser.newPage() page.goto the await ( 'https://account.aliyun.com/register/register.htm', {waitUntil: 'networkidle2'}) // Wait for page load Ends // targeting Frame const the await page.frames Frame = (). Find (Frame => frame.url () .includes ( 'https://passport.aliyun.com' )) // targeting verification slider const span the await frame.waitForSelector = ( '# nc_1_nlz' ) const spanInfo = await span.boundingBox() console.log(spanInfo) const div = await frame.waitForSelector('div#nc_1_scale_text > span') const divInfo = await div.boundingBox() await page.mouse.move(spanInfo.x, spanInfo.y) await page.mouse.down() // 鼠标移动 for (let i = 0, width = divInfo.width; i < width; i++) { await page.mouse.move(spanInfo.x + i, spanInfo.y) } // Release the mouse await page.mouse.up () } fun()
7、
---