Alex
Alex

Reputation: 15

Puppeteer - Get a page object from the 'browser.pages()' array?

During testing, a link that is clicked on could open in a new tab. I would like to get that page object, bring it to the front, and then take screenshots of it.

How do I get the page object of the last page in the pages array?

const pages = await browser.pages()
const newPage = pages[-1]
await newPage.bringToFront()

I am getting the error:

ReferenceError: newPage is not defined

Upvotes: 0

Views: 1016

Answers (1)

vadimk7
vadimk7

Reputation: 8303

-1 arrays index is not valid in JavaScript so to get the last element you can do the following:

const pages = await browser.pages()
const newPage = pages[pages.length - 1];
await newPage.bringToFront()

Upvotes: 1

Related Questions