puppeteer : 使用具有 dom 交互的外部函数
puppeteer : use an external function with dom interaction
使用 puppeteer chrome 测试开发工具,我想设置和使用外部函数
const puppeteer = require('puppeteer');
async function setSelectVal(page, sel, val) {
page.evaluate((data) => {
return document.querySelector(data.sel).value = data.val
}, {sel, val})
}
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('url', {waitUntil: 'load'});
const inputLogin = await page.$('input[name=login]');
await page.focus('input[name=login]');
await page.type('login', {delay: 100});
const inputPassord = await page.$('input[name=password]');
await inputPassord.click();
await page.type('pass', {delay: 100});
const inputValidate = await page.$('input[name=valid]');
await inputValidate.click();
await page.waitForSelector('td.newpageclass');
await setSelectVal(page, 'select[name=targetname]', 'targetvalue')
})();
但它触发了一个错误:
(node:16312) UnhandledPromiseRejectionWarning: Unhandled promise
rejection (rejection id: 1): Error: Protocol error (Runtime.evaluate):
Target closed. (node:16312) [DEP0018] DeprecationWarning: Unhandled
promise rejections are deprecated. In the future, promise rejections
that are not handled will terminate the Node.js process with a
non-zero exit code.
你知道我该怎么做吗?
只是范围界定问题:
您的 setSelectVal 函数没有 page 变量,请确保将其传递给它。
const puppeteer = require('puppeteer');
async function setSelectVal(page, sel, val) {
return await page.evaluate((data) => {
return document.querySelector(data.sel).value = data.val
}, {sel, val})
}
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('url', {waitUntil: 'load'});
await setSelectVal(page, 'select[name=targetname]', 5)
})();
使用 puppeteer chrome 测试开发工具,我想设置和使用外部函数
const puppeteer = require('puppeteer');
async function setSelectVal(page, sel, val) {
page.evaluate((data) => {
return document.querySelector(data.sel).value = data.val
}, {sel, val})
}
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('url', {waitUntil: 'load'});
const inputLogin = await page.$('input[name=login]');
await page.focus('input[name=login]');
await page.type('login', {delay: 100});
const inputPassord = await page.$('input[name=password]');
await inputPassord.click();
await page.type('pass', {delay: 100});
const inputValidate = await page.$('input[name=valid]');
await inputValidate.click();
await page.waitForSelector('td.newpageclass');
await setSelectVal(page, 'select[name=targetname]', 'targetvalue')
})();
但它触发了一个错误:
(node:16312) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Protocol error (Runtime.evaluate): Target closed. (node:16312) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
你知道我该怎么做吗?
只是范围界定问题: 您的 setSelectVal 函数没有 page 变量,请确保将其传递给它。
const puppeteer = require('puppeteer');
async function setSelectVal(page, sel, val) {
return await page.evaluate((data) => {
return document.querySelector(data.sel).value = data.val
}, {sel, val})
}
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('url', {waitUntil: 'load'});
await setSelectVal(page, 'select[name=targetname]', 5)
})();