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
Код кукловода JavaScript работает в режиме отладки, но не при запуске с использованием узла JS ⇐ Javascript
Форум по Javascript
1739167281
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-mercury-amd-radeon-rx-7900-xtx-magnetic-air-24gb-gddr6-pci-express-4-0-gaming-graphics-card-black-red/6588232.p?skuId=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
Подробнее здесь: [url]https://stackoverflow.com/questions/79426183/javascript-puppeteer-code-works-in-debug-mode-but-not-when-running-using-node-j[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия