Puppeteer get url of webpage opened in new tab

Viewed 3416

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) 
2 Answers

You can use page.evaluate to retrieve any information from web page context:

const url = await newPage.evaluate(() => document.location.href);
console.log(url);

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

const url = await page.url();
console.log(url);
Related