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

140
Visualizações
Get specific part of querySelector(x).innerText

So I'm trying to automate a task in Puppeteer, and one of the things I want to do involves getting the time as text. I found the original way too complicated so I opted for this method as the Date and Time is already there, just have to isolate the time.

Running this in the web console:

document.querySelector('div[class="h1 text-success"]').innerText;

gets me this (for this time, next time I go through the process, it'll show a different time assuming I refill out the form and the time updates).

'Tuesday, October 19, 2021 5:48 PM'

The Question: Is there any way to specifically isolate the 5:48 PM part no matter what comes before the time?

EDIT: The two methods posted do work (technically, assuming you're entering it into the web console yourself.) but Puppeteer's having issues (or I just have no clue how to implement it in Puppeteer, probably the latter).

Update 2 (because somehow I couldn't put 1 and 1 together quickly): instead of declaring the const TimeRX, I just removed it and just substituted TimeRX for the RegExp. This was what that line ended up as:

const time = await page.evaluate(()=>document.querySelector("div.h1.text-success").textContent.match(/\d{1,2}:\d{2} (AM|PM)/i)?.[0]);

I'll probably end up changing the RegExp for the other recommended one.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Capture it with a regex?

const twelveHourTimeRx = /(1[0-2]|(?<!\d)[1-9]):[0-5]\d ?(AM|PM)/i
// or the much simpler but less valid /\d{1,2}:\d{2} (AM|PM)/i

const time = document.querySelector("div.h1.text-success")
  .textContent.match(twelveHourTimeRx)?.[0]
  
console.log("time", time)
<div class="h1 text-success">
  Tuesday, October 19, 2021 5:48 PM
</div>

Note that I've used a much more forgiving selector to locate your <div>. Your original one requires the class attribute to be exactly "h1 text-success" but using .h1.text-success means that there can be other classes and in any order.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can split the string by a space and get the last two items:

const str = document.querySelector('.h1.text-success').textContent
const res = str.split(' ').slice(-2).join(' ')

console.log(res)
<div class="h1 text-success">Tuesday, October 19, 2021 5:48 PM</div>

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