Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda