1

I am trying to get the textContent of the last h4 on a page. Following is my code snippet. when I console.log models.length it is 0. There are actually 39 h4s on the page.

Please advise

    page.waitForNavigation({ waitUntil: 'networkidle0' })
    let models = [];
    models = await page.evaluate(() => Array.from(document.getElementsByTagName('h4'), e => e.textContent));

    const numModels = models.length;
    const fileName = models[numModels - 1];

``
skyboyer
  • 22,209
  • 7
  • 57
  • 64
Steven Greenbaum
  • 311
  • 4
  • 17

2 Answers2

1

This finally worked.

const h4All = await page.$$('h4');
const h4Count = h4All.length;
const fileName = h4All[h4Count - 1];
Steven Greenbaum
  • 311
  • 4
  • 17
  • Yes this is the solution. I saw you posted many questions related with scrap. May be i can help you in those part. Contact me if you have another issue. – i-Guru May 17 '20 at 18:22
0

In my side this works

const result = await page.evaluate(() => {
    let score = document.querySelector('#tableInfo50 > tbody > tr:nth-child(3) > 
                 td.text-left.bold-text').innerText
    return {
         score
    }
})
i-Guru
  • 164
  • 4
  • 25
  • I want the last h4. textContent. I don’t know in advance how may h4s there are. Also h4 are in a long chain of divs – Steven Greenbaum May 16 '20 at 19:05
  • This is the selector of the 14th element in Google dev tools. #underline-company > div > div > div > div:nth-child(14) > div > div > div > h4 As I said I don't want the 14th, I want the last. In dev-tools $('#underline-company > div > div > div > div:nth-child(14) > div > div > div > h4').innerText yields undefined. – Steven Greenbaum May 16 '20 at 19:28
  • This works as jquery selector in dev-tools $('#underline-company > div > div > div > div:nth-child(14) > div > div > div > h4').html() ---But how do I get last one, – Steven Greenbaum May 16 '20 at 19:39
  • Hi Steven, this is weekend. If you have skype lets talk there. I will take care and try to solve via team viewer – i-Guru May 17 '20 at 12:29
  • Hi iGuru, Thanks for the offer. I came up with an answer which I will post below. – Steven Greenbaum May 17 '20 at 17:32