Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer get url of webpage opened in new tab

I'm writing a web scraper to verify dates of sports events. One website is not showing sport event url on the listing, but instead link like: www.domain.com/redirectsystem/id12345 etc, once clicked new tab is being opened with the event website. What I want to achieve is to get url of this website opened in new tab.

I have managed to click the link with puppeteer, website is being opened in new tab but I don't know how to get the url information

 const browser = await puppeteer.launch({
   headless: false,
   'args' : [
          '--no-sandbox',
          '--disable-setuid-sandbox'
        ]
  });

  const newPagePromise = new Promise(x => browser.once('targetcreated', target => x(target.page())));
      await page.click('span.link-hightlight');

      const newPage = await newPagePromise; 
      const url = await newPage.url() // this is not working 
      console.log(url) 
like image 321
luk Avatar asked Feb 04 '26 20:02

luk


1 Answers

You can simply use page.url() to get the page URL

const url = await page.url();
console.log(url);
like image 63
Benjamin Ajewole Avatar answered Feb 06 '26 08:02

Benjamin Ajewole