从 jQuery 提取参数到 JavaScript 函数
Extract parameter from jQuery to JavaScript function
我目前正在使用 Puppeteer,每当我调用这个函数时,"selector" 是未定义的。
async function verifyTextPresent(page, selector){
let myButton = await page.evaluate(() => document.querySelector(selector).innerText);
console.log(myButton);
}
错误:
(node:6996) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: ReferenceError: selector is not defined
at <anonymous>:1:20
(node:6996) [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.
您需要像这样将选择器传递给评估:
async function verifyTextPresent(page, selector){
let myButton = await page.evaluate((selector) => {
document.querySelector(selector).innerText);
}
console.log(myButton);
}
我也更喜欢使用 $eval 的快捷方式:
let myButton = await page.$eval(selector, e => e.innerText);
我最终得到了这个解决方案,改变了它获取文本的方式:
async function verifyTextPresent(page, selector){
let myButton= await page.$(selector);
let selectorText = await (await elementForText.getProperty('innerText')).jsonValue();
console.log(selectorText);
}
我目前正在使用 Puppeteer,每当我调用这个函数时,"selector" 是未定义的。
async function verifyTextPresent(page, selector){
let myButton = await page.evaluate(() => document.querySelector(selector).innerText);
console.log(myButton);
}
错误:
(node:6996) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: ReferenceError: selector is not defined
at <anonymous>:1:20
(node:6996) [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.
您需要像这样将选择器传递给评估:
async function verifyTextPresent(page, selector){
let myButton = await page.evaluate((selector) => {
document.querySelector(selector).innerText);
}
console.log(myButton);
}
我也更喜欢使用 $eval 的快捷方式:
let myButton = await page.$eval(selector, e => e.innerText);
我最终得到了这个解决方案,改变了它获取文本的方式:
async function verifyTextPresent(page, selector){
let myButton= await page.$(selector);
let selectorText = await (await elementForText.getProperty('innerText')).jsonValue();
console.log(selectorText);
}