Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
How to open and control a new tab TARGET _BLANK by click puppeteer

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda