there is a button that open a new tab (_blank) in browser, how do i reference this(like "await page2...") new tab and control it?
<button id="button" href="randomCode" target="_blank">BUTTON</button>
I tried use this in puppeteer:
await page1.waitForSelector('#button');
const link = await page1.$('#button');
const newPagePromise = new Promise(x => browser.once('targetcreated', target => x(target.page())));
await link.click({button: 'left'});
const page2= await newPagePromise;
await page2.bringToFront();
But in this case, it only work to control sometimes (I don’t know why)
Note: The button link change with time, so it's necessary to click in the button/ can't close the first page.
If there is someone with the same error, I found the solution, get the href of button, and then, open a new tab with the href:
const hrefs1 = await page.evaluate(
() => Array.from(
document.querySelectorAll('a[SELECTOR]'),
a => a.getAttribute('href')
)
);
const pagina2 = await browser.newPage();
await pagina2.goto(''+hrefs1);
There is no error in this case.