Javascript 错误:计算失败:无效参数:应正好是一个字符串。|puppeter.evaluate()错误

Javascript 错误:计算失败:无效参数:应正好是一个字符串。|puppeter.evaluate()错误,javascript,puppeteer,Javascript,Puppeteer,所以这是我第一次使用木偶演员进行测试。但我在运行时遇到了一个奇怪的错误 但是,我要做的是在页面中运行这个函数 const puppeteer = require('puppeteer'); let browser, page, //evaluates to file://C:/Users/chris/Google Drive/code projects/text translation sheet/test/src/index.html htmlFilepath = "f

所以这是我第一次使用木偶演员进行测试。但我在运行时遇到了一个奇怪的错误

但是,我要做的是在页面中运行这个函数

const puppeteer = require('puppeteer');   
let browser, page,
//evaluates to file://C:/Users/chris/Google Drive/code projects/text translation sheet/test/src/index.html
    htmlFilepath = "file://" + path.resolve(__dirname, "src/index.html");
browser = await puppeteer.launch();
page = await browser.newPage();
await page.goto(htmlFilepath);   
page.exposeFunction(getMaxWordsInContentArea.name, getMaxWordsInContentArea)
      
const lastWordThatFit = await page.evaluate(function(){
          
    const el = document.querySelector('.line__target-language-text')
    const elContainer = el.parent;
    const lastFitWordIndex = getMaxWordsInContentArea(el,elContainer,["Call", "me", "Ishmael.", "Some", "years", "ago—never", "mind", "how", "long", "precisely—having", "little", "or", "no", "money", "in", "my", "purse,", "and", "nothing", "particular", "to", "interest", "me", "on", "shore,", "I", "thought", "I", "would", "sail", "about", "a", "little", "and", "see", "the", "watery", "part", "of", "the", "world.", "It", "is", "a", "way", "I", "have", "of", "driving", "off", "the", "spleen", "and", "regulating", "the", "circulation."])  
                    
    return lastFitWordIndex
});

有人看到此错误或熟悉此错误吗?

忘记在
页面前面包含
等待
。exposeFunction(getMaxWordsInContentArea.name,getMaxWordsInContentArea)


向@ZenMonkey大喊,感谢他推动我深入挖掘。

忘了在
页面前面包含
等待
。exposeFunction(getMaxWordsInContentArea.name,getMaxWordsInContentArea)


向@ZenMonkey大喊,因为它促使我深入挖掘。

您正在将一个函数传递到
.evaluate()
调用中,它需要一个字符串。我会在别处定义这个函数,然后像
.evaluate(myFunction())
那样调用它,但它不起作用。函数内部的逻辑需要在页面内部执行。要在函数位于页面内部时获取要调用的函数,需要使用
evaluate
函数。
evaluate()
假定允许根据此处的文档传递函数:。还有什么看起来可能会引起问题的吗?我明白了,它是用来评估你传递给它的函数的。我会使用lambda函数。这里的答案几乎向您展示了如何使用它—您正在将函数传递到它需要字符串的
.evaluate()
调用中。我会在别处定义这个函数,然后像
.evaluate(myFunction())
那样调用它,但它不起作用。函数内部的逻辑需要在页面内部执行。要在函数位于页面内部时获取要调用的函数,需要使用
evaluate
函数。
evaluate()
假定允许根据此处的文档传递函数:。还有什么看起来可能会引起问题的吗?我明白了,它是用来评估你传递给它的函数的。我会使用lambda函数。这个答案告诉你如何使用它