Код кукловода JavaScript работает в режиме отладки, но не при запуске с использованием узла JSJavascript

Форум по Javascript
Ответить
Anonymous
 Код кукловода JavaScript работает в режиме отладки, но не при запуске с использованием узла JS

Сообщение Anonymous »

const puppeteer = require('puppeteer');

(async () => {
// Launch a new browser instance
const browser = await puppeteer.launch({ headless: false }); // Set headless to false to see the browser
const page = await browser.newPage();

// URL of the page to load
const url = 'https://www.bestbuy.com/site/xfx-mercur ... Id=6588232';

// Open the page
await page.goto(url, { waitUntil: 'domcontentloaded' });

// Define the selector for the button you are looking for
const buttonSelector = '.c-button.c-button-primary.c-button-lg.c-button-block.c-button-icon.c-button-icon-leading.add-to-cart-button '; // Replace with your button's selector

// Wait for the button to appear or reload the page
while (true) {
try {
// Try to find the button
await page.waitForSelector(buttonSelector, { visible: true, timeout: 50000 });
console.log('Button found!');
break; // Exit the loop once the button is found
} catch (error) {
console.log('Button not found, reloading the page...');

// Reload the page if the button is not found
await page.reload({ waitUntil: 'domcontentloaded' });
}
}

// Close the browser
await browser.close();
})();
< /code>
Я пытаюсь перезагрузить веб -страницу, пока на экране не будет найдена кнопка. Этот код работает нормально, когда в режиме отладки в VSCODE, но когда я пытаюсь запустить его, используя Node JS, он дает мне ошибку. Вывод моей программы консоли, который ожидается, но также также ошибкой. < /p>
$ node test.js Button not found, reloading the page...
< /code>
C:\Users\student\Desktop\BestBuyTracker\BestBuyTracker\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\CDPSession.js:67return Promise.reject(new Errors_js_1.TargetCloseError(Protocol error (${method}): Session closed. Most likely the ${this.#targetType} has been closed.));^ TargetCloseError: Protocol error (Page.reload): Session closed. Most likely the page has been closed.at CdpCDPSession.send (C:\Users\student\Desktop\BestBuyTracker\BestBuyTracker\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\CDPSession.js:67:35) at CdpPage.reload (C:\Users\student\Desktop\BestBuyTracker\BestBuyTracker\node_modules\puppeteer-core\li b\cjs\puppeteer\cdp\Page.js:667:39) at C:\Users\student\Desktop\BestBuyTracker\BestBuyTracker\test.js:28:18 at process.processTicksAndRejections (node:internal/process/task_queues:105:5) Node.js v22.13.1


Подробнее здесь: https://stackoverflow.com/questions/794 ... ing-node-j
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»