javascript - 错误评估失败 - 试图将对象传递给 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} 也没有运气。
解决方案
在 puppeteer 评估中无法访问 userInfo
您可以使用带有推送参数的评估方法,请参见此处Puppeteer: pass variable in .evaluate()
推荐阅读
- java - 如何从扩展活动覆盖方法?
- python - 站立的选择不做任何事
- python - ModuleNotFoundError:没有名为“脚本”的模块,但脚本实际存在且目录精确
- c# - 如何使用主类更改子类中的选定元素?
- c++ - Cmake Gtest 项目 exe 以错误代码 255 退出
- excel - 在第 2 列中查找特定值并使用 excel 区分其所有第 1 列
- rest - java.lang.IllegalStateException:没有使用 Spring Boot 和 OAuth2 设置 ServletContext 异常
- c++ - 防止 QSettings 将组织名称添加到 QSettings::setPath() 设置的路径中
- android - 从 Firebase 控制台中删除 Firebase 用户时(用户登录后),它不会反映在应用程序中
- ms-access - 在 ms 访问中创建动态查询