错误评估失败-尝试将对象传递到操纵up功能

时间:2020-04-29 20:29:24

标签: javascript node.js error-handling async-await 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}也不走运。

1 个答案:

答案 0 :(得分:3)

在伪造者评估中无法访问

userInfo

您可以将评估方法用于推入参数,请参见Puppeteer: pass variable in .evaluate()