I’m working on a web scraping project where I need to click a link that opens in a new tab, then get the title from that newly opened tab using puppeteer.
Here’s my current code:
const puppeteer = require('puppeteer');
const delay_time = 2000;
async function scrapeTitle() {
const browser = await puppeteer.launch({
headless: false
});
const mainPage = await browser.newPage();
await mainPage.goto('https://example-blog.com/article/mixed-models-guide/');
const linkSelector = '#article-123 > div > section.content > p:nth-child(3) > a:nth-child(1)';
await mainPage.waitFor(delay_time);
await mainPage.click(linkSelector);
// After clicking, a new tab opens
let allPages = await browser.pages();
// Need to switch to the new tab and get its title
// This is where I'm stuck
}
scrapeTitle();
The issue I’m having is figuring out how to properly access the newly created tab after clicking the link. I know I need to use the pages array but I’m not sure how to target the specific new page and extract its title. The new tab should contain a page with the title ‘Advanced Mixed Models Tutorial’ but I can’t seem to access it properly.