提问者:小点点

无法将参数传递到评估函数


我试图将background-color作为一个参数传递给求值函数,如下面代码所示:

const loc = await page.waitForSelector("#Login");
const style = 'background-color';
const thecssStyle = await page.evaluate(el =>
  window.getComputedStyle(el).getPropertyValue(style),loc
);
console.log("Print :"+thecssStyle);

这不起作用,我得到以下错误:

UnhandledPromiserEjectionWarning:错误:求值失败:ReferenceError:未定义样式

但是如果我直接传递背景颜色,就像下面所示的那样,它的效果绝对很好:

const loc = await page.waitForSelector("#Login");
const thecssStyle = await page.evaluate(el =>
  window.getComputedStyle(el).getPropertyValue('background-color'),loc
);
console.log("Print :"+thecssStyle);

我想把background-color作为参数传递,而不是直接传递它,我该怎么做呢?


共1个答案

匿名用户

您应该以与传递loc相同的方式传递样式

const loc = await page.waitForSelector("#Login");
const style = 'background-color';
const thecssStyle = await page.evaluate(
  (el, style) => window.getComputedStyle(el).getPropertyValue(style),
  loc,
  style
);
console.log("Print :"+thecssStyle);