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

147
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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