首页 > 解决方案 > 错误评估失败 - 试图将对象传递给 puppeteer 函数

问题描述

当试图将一个简单的对象传递给运行 puppeteer 的异步函数时,我收到此错误:

(node:4000) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: userInfo is not defined
    at __puppeteer_evaluation_script__:1:19
    at ExecutionContext._evaluateInternal (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\ExecutionContext.js:93:19)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async ExecutionContext.evaluate (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\ExecutionContext.js:32:16)
    at async ElementHandle.evaluate (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\JSHandle.js:39:16)
    at async ElementHandle.$eval (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\JSHandle.js:372:24)
    at async checkout (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\backend\safeBackend.js:93:3)
    at async startSafeBot (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\backend\safeBackend.js:162:3)            

这是我的代码:

const checkout = async (page, userInfo) => {
  await page.click('#checkout-now'); // click checkout now after adding to cart is done
  console.log("Added to cart, going to checkout...");

  // waits for form to load
  let checkoutFormVisible = await isElementVisible(page, '#order_bn');

  while (!checkoutFormVisible) {
    checkoutFormVisible = await isElementVisible(page, '#order_bn');
  }

  console.log("Checkout loaded, filling payment...");

  // filling in billing
  await page.$eval('#order_bn', el => el.value = userInfo.name);
  await page.$eval('#order_email', el => el.value = userInfo.email);
  await page.$eval('#order_tel', el => el.value = userInfo.phoneNumber);
  await page.$eval('#order_billing_address', el => el.value = userInfo.billingAddress1);
  await page.$eval('#order_billing_address_2', el => el.value = userInfo.billingAddress2);
  await page.$eval('#obz', el => el.value = userInfo.zipCode);
  await page.$eval('#order_billing_city', el => el.value = userInfo.billingCity);
  await page.select('#order_billing_state', userInfo.billingState);
  await page.select('#order_billing_country', userInfo.billingCountry);
  await page.$eval('#cnid', el => el.value = userInfo.creditCardNum); // this is a prepaid card with like 30 cents on it
  await page.select('#credit_card_month', userInfo.creditCardMonth);
  await page.select('#credit_card_year', userInfo.creditCardYear);
  await page.$eval('#vvv-container > input[type=tel]', el => el.value = userInfo.cvv);
  await page.click("#order_terms");
  await page.$eval('#g-recaptcha-response', el => el.value = userInfo.gRecaptchaRes);
  await wait(DELAY); // wait a little before button is clicked
  await page.click("#submit_button");
  await page.waitForSelector('#checkout-loading-message > span > span'); // let payment processing page load before checking

async function startSafeBot () {

  const browser = await puppeteer.launch({ 
    headless: false,
  });

  const browserPage = await browser.newPage();

  const userData = {
    'name': 'frank ied223213',
    'email': 'frankied324234234@gmail.com',
    'phoneNumber': '914-123-1234',
    'billingAddress1': '123 testing lane',
    'billingAddress2': '',
    'zipCode': '12345',
    'billingCity': 'test',
    'billingState': 'NY',
    'billingCountry': 'USA',
    'creditCardNum': '12323123123123123213',
    'creditCardMonth': '04',
    'creditCardYear': '2025',
    'cvv': '123',
    'gRecaptchaRes': "3e232d32cf4d34343d434132d4124c234d24"
  };

  await generateSupremeBrowser(browserPage,1,"beaded","Black","N/A");
  await addToCart(browserPage);
  await checkout(browserPage, userData);
  await processPayment(browserPage);
  await browser.close();
}

startSafeBot();

我试过先声明 userinfo,然后给它赋值。没用。console.log("checkout loaded, filling payment...")此外,当我在用户信息上设置断点时, 定义了我分配给它的所有值。这很奇怪,因为它已经定义了。我相信这与傀儡师有关。我也尝试过 ${userInfo.name} 也没有运气。

标签: javascriptnode.jserror-handlingasync-awaitpuppeteer

解决方案


在 puppeteer 评估中无法访问 userInfo

您可以使用带有推送参数的评估方法,请参见此处Puppeteer: pass variable in .evaluate()


推荐阅读