无法让我的脚本继续点击按钮
Unable to make my script keep clicking on a button
我使用 node.js
和 puppeteer
创建了一个脚本来点击 more
按钮位于网页底部,用于从其着陆页中挖掘出所有标题。
事情是当我执行我的脚本时,它只点击一次然后退出。我怎样才能一直点击那个按钮,直到没有更多的按钮可以点击,这意味着所有的链接都显示出来了?
我目前的尝试:
const puppeteer = require("puppeteer");
(async function main() {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.goto("https://www.newsnow.co.uk/h/Sport/Football/Championship/Transfer+News", {waitUntil: 'networkidle2'});
await page.waitForSelector("[class^='hl_more']");
await page.click("[class^='hl_more']");
await browser.close();
})();
由于我是 node.js
和 puppeteer
的新手,我无法理解如何定义循环来完成任务。
只需添加一个点击命令的循环,例如:
const puppeteer = require("puppeteer");
(async function main() {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.goto("https://www.newsnow.co.uk/h/Sport/Football/Championship/Transfer+News", {waitUntil: 'networkidle2'});
await page.waitForSelector("[class^='hl_more']");
while (1) { // This one
await page.click("[class^='hl_more']");
}
await browser.close();
})();
在这种情况下,我经常做的是使用 try catch
块使用 waitForSelector
并使用非常短的超时来检查元素的可见性。您需要 try catch
块,因为当 more 按钮不再可见时,waitForSelector
最终会超时。这也是为什么您需要使用一个简短的特定超时,因为您不希望您的代码在尝试查找组件时暂停 30 秒(默认超时)。
所以我要做的是:
// This method checks if an element is visible and times out cleanly after 2 seconds if it is not displayed
const isElementVisible = async (page, cssSelector) => {
try {
await page.waitForSelector(cssSelector, { timeout: 2000 });
} catch {
return false;
}
return true;
};
(async function main() {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.goto("https://www.newsnow.co.uk/h/Sport/Football/Championship/Transfer+News", {waitUntil: 'networkidle2'});
let moreDisplayed = await isElementVisible(page, '[class^="hl_more"]');
while (moreDisplayed) {
await page.click('[class^="hl_more"]');
moreDisplayed = await isElementVisible(page, '[class^="hl_more"]');
}
await browser.close();
})();
我使用 node.js
和 puppeteer
创建了一个脚本来点击 more
按钮位于网页底部,用于从其着陆页中挖掘出所有标题。
事情是当我执行我的脚本时,它只点击一次然后退出。我怎样才能一直点击那个按钮,直到没有更多的按钮可以点击,这意味着所有的链接都显示出来了?
我目前的尝试:
const puppeteer = require("puppeteer");
(async function main() {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.goto("https://www.newsnow.co.uk/h/Sport/Football/Championship/Transfer+News", {waitUntil: 'networkidle2'});
await page.waitForSelector("[class^='hl_more']");
await page.click("[class^='hl_more']");
await browser.close();
})();
由于我是 node.js
和 puppeteer
的新手,我无法理解如何定义循环来完成任务。
只需添加一个点击命令的循环,例如:
const puppeteer = require("puppeteer");
(async function main() {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.goto("https://www.newsnow.co.uk/h/Sport/Football/Championship/Transfer+News", {waitUntil: 'networkidle2'});
await page.waitForSelector("[class^='hl_more']");
while (1) { // This one
await page.click("[class^='hl_more']");
}
await browser.close();
})();
在这种情况下,我经常做的是使用 try catch
块使用 waitForSelector
并使用非常短的超时来检查元素的可见性。您需要 try catch
块,因为当 more 按钮不再可见时,waitForSelector
最终会超时。这也是为什么您需要使用一个简短的特定超时,因为您不希望您的代码在尝试查找组件时暂停 30 秒(默认超时)。
所以我要做的是:
// This method checks if an element is visible and times out cleanly after 2 seconds if it is not displayed
const isElementVisible = async (page, cssSelector) => {
try {
await page.waitForSelector(cssSelector, { timeout: 2000 });
} catch {
return false;
}
return true;
};
(async function main() {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.goto("https://www.newsnow.co.uk/h/Sport/Football/Championship/Transfer+News", {waitUntil: 'networkidle2'});
let moreDisplayed = await isElementVisible(page, '[class^="hl_more"]');
while (moreDisplayed) {
await page.click('[class^="hl_more"]');
moreDisplayed = await isElementVisible(page, '[class^="hl_more"]');
}
await browser.close();
})();