隨着操縱木偶的鉻試驗開發工具,我想設置和使用外部FUNC木偶戲:使用外部功能與DOM的互動
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.
你知道我應該怎麼做?
沒有太多這方面的專家,但好像browser.newPage()失敗,做一個嘗試捕捉並檢查異常,甚至聽起來頁面爲空(即browser.newPage()或者不一個承諾或返回空? – PiniH
@PiniH謝謝fr answere,非newage作品我編輯頁面,以顯示案件更多細節 –