Catch second row of a table

Asked

Viewed 242 times

0

I’m developing an application using Node.js and puppeter to take data from a web page, the problem I have and that I’m not getting the columns I want inside the selected row, this is the part of the code I’m having difficulties with:

 async function buscaFases(frame) {
    return await frame.evaluate(() => {
        let div = document.querySelector('div[id*="conteudo"]');
        let rowns = Array.from(div.children[4].children[0].children);
        let movimentosInfo = rowns.map(row => {
             let data = row.querySelector("tr td:first-child").textContent;
      let descricao = row.querySelector("tr td:first-child + td").textContent;
          return { data, descricao };
        });
        return JSON.stringify(movimentosInfo);
    });
};

The code works as expected until the selection from row to row but when it goes to get the information from the column I want it returns null.

Follow the code Complete, with the url to the page and relevant information:

const fs = require('fs');
const puppeteer = require('puppeteer');

const urlConsulta = "http://www.tre-pr.jus.br/";
const numeroProcessoSeq = "000000889";
const numeroProcessoAno = "2014";
const numeroProcessoDigito = "6160047";

var wait = ms => new Promise((r, j)=> setTimeout(r, ms));

void (async () => {
    try {
        const browser = await puppeteer.launch({
            headless: false
        });
        const page = await browser.newPage();
        await page.goto(urlConsulta);
        await page.select('#acao', 'pesquisarNumUnico');
        await page.evaluate((numeroProcessoSeq, numeroProcessoAno, numeroProcessoDigito) => {
            document.getElementById('numUnicoSequencial').value = numeroProcessoSeq;
            document.getElementById('numUnicoAno').value = numeroProcessoAno;
            document.getElementById('numUnicoOrigem').value = numeroProcessoDigito;
        }, numeroProcessoSeq, numeroProcessoAno, numeroProcessoDigito);

        await page.$eval('form[action*="http://www.tre-pr.jus.br/@@processrequest"]', form => form.submit());

        await page.waitForNavigation();
        var frame = await page.frames().find(f => f.name() === 'ifr_servicos');
        await frame.click('a[href*="ExibirDadosProcesso"]');
        await page.frames().find(f => f.name() === 'ifr_servicos');
        await wait(10000);
        await frame.click('[name*="todos"]');
        await frame.$eval('[name*="ExibirPartesProcessoZona"]', form => form.submit());
        await wait(10000);
        let string = await buscaFases(frame);
        fs.writeFile("teste.txt", string, function(err) {
            if(err) {
                return console.log(err);
            }
            console.log("The file was saved!");
        }); 
        console.log(string);
        await wait(10000);
        await browser.close();
    } catch (error) {
        console.log(error);
    }
})();

async function buscaFases(frame) {
    return await frame.evaluate(() => {
        let div = document.querySelector('div[id*="conteudo"]');
        let rowns = Array.from(div.children[4].children[0].children);
        let movimentosInfo = rowns.map(row => {
            let data = row.querySelector("tr > td + td").textContent;
            let descricao = row.querySelector("tr > td + td + td").textContent;
          return { data, descricao };
        });
        return JSON.stringify(movimentosInfo);
    });
};

1 answer

0

I found the solution, the problem is that not all the lines had the elements in the order I wanted so it is necessary to filter only the lines that have what I want:

let movimentosInfo = rowns.filter(row => {
    return row.querySelector("tr td:first-child") && row.querySelector("tr td:first-child + td");
}).map(row => {
    let data = row.querySelector("tr td:first-child").textContent;
    let descricao = row.querySelector("tr td:first-child + td").textContent;
    return { data, descricao };
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.