Javascript 使用Puppeter的page.eval函数上未定义的变量
当我在代码中运行它时,它表示dottt是未定义的,但是为什么它正确地接受了pdfcitystatezipselector。如果我用一个字符串文字替换dottt,它就工作得很好Javascript 使用Puppeter的page.eval函数上未定义的变量,javascript,bots,eval,puppeteer,headless,Javascript,Bots,Eval,Puppeteer,Headless,当我在代码中运行它时,它表示dottt是未定义的,但是为什么它正确地接受了pdfcitystatezipselector。如果我用一个字符串文字替换dottt,它就工作得很好 const pdfcitystatezipselector = 'body > div:nth-child(1) > div:nth-child(6) > span'; const dottt = "111111"; await page.$eval(pdfdotselector, (eleme
const pdfcitystatezipselector = 'body > div:nth-child(1) > div:nth-child(6) > span';
const dottt = "111111";
await page.$eval(pdfdotselector, (element1, dotttt) => {
element1.innerHTML = dottt;
});
您需要将
dottt
参数传递给$eval
函数:
constpdfcitystatezipselector='body>div:n个孩子(1)>div:n个孩子(6)>span';
const dottt=“111111”;
等待页面$eval(pdfdotselector,
(元素1,dottt)=>{
element1.innerHTML=dottt;
},
dottt);//在这里
我假设dottt
应该是dottt
——否则我就完全搞不懂了。请注意,有两个单独的变量称为dottt
。第一个被声明为const dottt=“111111”代码>。但是在函数的作用域中还有另一个(函数的参数),它覆盖了第一个。它是未定义的,可能是因为该函数仅使用一个参数调用。我只是尝试使用在函数内部的函数外部声明的变量。当我这样做时,它一直给我一个未定义的值。我尝试了你的精确代码,它仍然说“ReferenceError:dottt未定义”。我使用的代码如下。constpdfdotselector='body>div:nth child(1)>div:nth child(2)>span';const dottt=“111111”;等待页面。$eval(pdfdotselector,(element1,dottt)=>{element1.innerHTML=dottt;},dottt)代码>