Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!