Я пытаюсь автоматически заполнить форму с помощью кукловода, но она продолжает выдавать ReferenceError: text is not defined
, даже если она была определена 1 строкой ранее, а console.log
успешно ее отображал.
Это строка, которая выдает ошибку: await page.$eval('#chat-message-text', el => el.value = text);
Если я заполню ее чем-то вроде el.value = 'blablabla'
, она будет работать хорошо.
Самое странное, что console.log(text);
тоже работает хорошо, а затем выдает ошибку.
puppeteer.launch({ headless: false }).then(async browser => {
const page = await browser.newPage()
var cookie = [here are my cookies]
await page.setCookie(...cookie)
await page.goto('https://example.com')
await page.waitFor(5000);
await page.solveRecaptchas()
console.log('Captcha was solved');
await page.waitFor(5000);
var text = JSON.parse(fs.readFileSync('/Users/apple/Desktop/test/text.json', 'utf8'));
console.log("Sending text");
console.log(text);
await page.$eval('#chat-message-text', el => el.value = text);
await page.waitFor(1000);
await page.waitForSelector('button[class="btn btn-primary send-chat-btn"]');
await page.click('button[class="btn btn-primary send-chat-btn"]');
await page.waitFor(2000);
await browser.close()
})
полный журнал ошибок:
(node:86035) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: text is not defined
at __puppeteer_evaluation_script__:1:19
at ExecutionContext._evaluateInternal (/Users/apple/Desktop/test/node_modules/puppeteer/lib/ExecutionContext.js:122:13)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
-- ASYNC --
at ExecutionContext.<anonymous> (/Users/apple/Desktop/test/node_modules/puppeteer/lib/helper.js:111:15)
at ElementHandle.evaluate (/Users/apple/Desktop/test/node_modules/puppeteer/lib/JSHandle.js:54:42)
at ElementHandle.<anonymous> (/Users/apple/Desktop/test/node_modules/puppeteer/lib/helper.js:112:23)
at ElementHandle.$eval (/Users/apple/Desktop/test/node_modules/puppeteer/lib/JSHandle.js:505:40)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
-- ASYNC --
at ElementHandle.<anonymous> (/Users/apple/Desktop/test/node_modules/puppeteer/lib/helper.js:111:15)
at DOMWorld.$eval (/Users/apple/Desktop/test/node_modules/puppeteer/lib/DOMWorld.js:156:21)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
-- ASYNC --
at Frame.<anonymous> (/Users/apple/Desktop/test/node_modules/puppeteer/lib/helper.js:111:15)
at Page.$eval (/Users/apple/Desktop/test/node_modules/puppeteer/lib/Page.js:345:29)
at Page.<anonymous> (/Users/apple/Desktop/test/node_modules/puppeteer/lib/helper.js:112:23)
at puppeteer.launch.then (/Users/apple/Desktop/test/app/test2.js:151:14)
at <anonymous>
(node:86035) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:86035) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.