Open felix-cao opened 4 years ago
查询单个节点
selector
<string> A selector to query page forreturn
<Promise<?ElementHandle>>The method runs document.querySelector within the page. If no element matches the selector, the return value resolves to null
.
查询多个节点
selector
<string> A selector to query page forreturn
Promise<Array<ElementHandle>>>The method runs document.querySelectorAll
within the page. If no elements match the selector, the return value resolves to []
.
提取单个节点
selector
<string> A selector to query page forpageFunction
<function(Element)> Function to be evaluated in browser context...args
<...Serializable|JSHandle> Arguments to pass to pageFunction
returns
: <Promise<Serializable>> Promise which resolves to the return value of pageFunction
This method runs document.querySelector
within the page and passes it as the first argument to pageFunction
. If there's no element matching selector
, the method throws an error.
If pageFunction
returns a Promise, then page.$eval
would wait for the promise to resolve and return its value.
Examples:
const searchValue = await page.$eval('#search', (el) => el.value);
const preloadHref = await page.$eval('link[rel=preload]', (el) => el.href);
const html = await page.$eval('.main-container', (e) => e.outerHTML);
evaluate, 求值的意思。
提取多个节点
selector
<string> A selector to query page forpageFunction
<function(Array<Element>)> Function to be evaluated in browser context...args
<...Serializable|JSHandle> Arguments to pass to pageFunction
returns
: <Promise<Serializable>> Promise which resolves to the return value of pageFunction
This method runs Array.from(document.querySelectorAll(selector))
within the page and passes it as the first argument to pageFunction
.
If pageFunction
returns a Promise, then page.$$eval
would wait for the promise to resolve and return its value.
Examples:
const divCount = await page.$$eval('div', (divs) => divs.length);
const options = await page.$$eval('div > span.options', (options) =>
options.map((option) => option.textContent)
);
pageFunction
<function|string> Function to be evaluated in the page context...args
<...Serializable|JSHandle> Arguments to pass to pageFunction
returns
: <Promise<Serializable>> Promise which resolves to the return value of pageFunction
If the function passed to the page.evaluate
returns a Promise, then page.evaluate
would wait for the promise to resolve and return its value.
If the function passed to the page.evaluate
returns a non-Serializable value, then page.evaluate resolves to undefined. DevTools Protocol also supports transferring some additional values that are not serializable by JSON
: -0
, NaN
, Infinity
, -Infinity
, and bigint literals.
Passing arguments to pageFunction
:
const result = await page.evaluate((x) => {
return Promise.resolve(8 * x);
}, 7);
console.log(result); // prints "56"
A string can also be passed in instead of a function:
console.log(await page.evaluate('1 + 2')); // prints "3"
const x = 10;
console.log(await page.evaluate(`1 + ${x}`)); // prints "11"
ElementHandle instances can be passed as arguments to the page.evaluate
:
const bodyHandle = await page.$('body');
const html = await page.evaluate((body) => body.innerHTML, bodyHandle);
await bodyHandle.dispose();
一、配置类
1.1、创建一个浏览器对象
1.2、等待页面 dom 全部渲染完成后
二、监控类
2.1、监控页面 Dialog
不要在循环中使用
page.on
, 可以拆分或换为page.once
2.2、获取 Response
常规
ajax 轮训
当响应的
body
数据中body.SimulationProgress === 100
时返回三、页面输入设置
3.1、键盘输入效果
以百度搜索为例,在百度搜索输入框中输入:合肥创新产业园
清空表单里原有内容
参考 How to delete existing text from input using Puppeteer?
3.2、设置下拉选择框
3.3、设置和获取 localStorage
在使用
jest
进行但愿测试时,需要存储一些global
级别的, 但jest
提倡的是mock
数据,所以在单个test
文件里不支持对global
级别的变量进行update
, 使用页面级的localStorage
是个非常不错的变通方案。设置 localStrorage
获取 localStorage
3.4、传一个参数给页面执行
3.5、操作(读取和设置) checkbox 复选框
如果没有选择,则点击,否则不点击
所有的 checkbox 全选(没点击哦)
依次点击所有的 checkbox
四、页面数据读取
4.1、获取DOM文本数据
4.2、点击包含指定文本的DOM节点
方法一:使用page.$x
在百度首页,点击含有'更多'的
a
DOM
节点参考 How to click on element with text in Puppeteer
方法二:使用 page.evaluate
遍历DOM节点,比较 innerText 值
4.3、获取 class 属性数据
参考 elementHandle.getProperty(propertyName) 、elementHandle.jsonValue() 、How do I get the ElementHandle's class name when using Puppeteer?
4.4、获取 Cookies 数据
获取当前域下的所有
cookie
, 并把cookie
放到localStorage
中带 cookie 的 post 请求
4.5、获取单选框 radio 的值数据
4.6、获取页面 display 值
调用下面的函数
4.7、获取当前节点的下一个兄弟节点
使用 Element.nextElementSibling
参考 Getting the sibling of an elementHandle in Puppeteer
五、其他
5.1、上传下载
上传
下载
批量下载
注意上面的代码不能使用
forEach
5.2、控制打开的新窗口
主要是操作 window.open() 后的对象
5.3、抓取页面js错误、请求错误
请参考 How do I capture browser errors
Reference