easy-puppeteer
v0.2.2
Published
easy-puppeteer is a wrapper api of puppeteer for complex crawler app.
Downloads
1
Readme
easy-puppeteer
easy-puppeteer is a wrapper api of puppeteer for complex crawler app.
Install
$ npm install easy-puppeteer
Usage
const ez = require('easy-puppeteer');
API
operate(page, element)
- page <Puppeteer>
- element <ElementHandle>
safeClickForNavigation(page, element | selector)
Click a button or a hyperlink to navigate to a new page and wait it to be loaded.
- page <Puppeteer>
- element <ElementHandle> | selector
- returns <Promise>
// pass selector
await ez.safeClickForNavigation(page,".list-divider-pipe li:nth-child(2) > a");
// pass elementHandle
const element = await ez.safeGetElement(page,".list-divider-pipe li:nth-child(2) > a");
await ez.safeClickForNavigation(page, element);
safeClick(page, element | selector)
Click a button or a hyperlink but not trigger page to navigate.
- page <Puppeteer>
- element <ElementHandle> | selector
- returns <Promise>
// pass selector
await ez.safeClick(page, "#lang-picker-toggler");
// pass elementHandle
const el = await ez.safeGetElement(page, "#lang-picker-toggler");
await ez.safeClick(page, el);
doForEach(page, selector, operate, [waitOptions = {}])
Do some asynchronous operate on every element in elements array like forEach.
- page <Puppeteer>
- selector <String>
- operate <operate>
- waitOptions <Object>
- returns <Promise>
let languages = [];
await ez.doForEach(page, ".lang-picker li", async (page, element) => {
const label = await page.evaluate(el => el.innerText, element);
languages.push(label.trim());
});
safeGetElement(page, selector, [waitOptions = {}])
Wait element appearing on the DOM and return it.
- page <Puppeteer>
- selector <String>
- waitOptions <Object>
- returns <Promise>
const element = await ez.safeGetElement(page,".list-divider-pipe li:nth-child(2) > a");
safeGetElements(page, selector, [waitOptions = {}])
Wait elements appearing on the DOM and return them.
- page <Puppeteer>
- selector <String>
- waitOptions <Object>
- returns <Promise>
const elements = await ez.safeGetElements(page,".list-divider-pipe li > a");
mkElementsVisible(page, selector)
Make elements to be visible. This help you to do some further work like clicking.
- page <Puppeteer>
- selector <String>
await ez.mkElementsVisible(page, ".lang-picker");