从jQuery提取参数到JavaScript函数

时间:2017-11-27 14:52:01

标签: javascript jquery parameter-passing puppeteer

我目前正在使用Puppeteer,每当我调用此函数时,"选择器"未定义。



async function verifyTextPresent(page, selector){
  let myButton = await page.evaluate(() => document.querySelector(selector).innerText);
  console.log(myButton);
}




ERROR:

(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.

2 个答案:

答案 0 :(得分:0)

您需要将选择器传递给评估,如下所示:

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);

答案 1 :(得分:0)

我最终得到了这个改变锯子的解决方案,它得到了文字:

async function verifyTextPresent(page, selector){
  let myButton= await page.$(selector);
  let selectorText = await (await elementForText.getProperty('innerText')).jsonValue();
      console.log(selectorText);
    }