-2
I need to download or save the PDF file from a built-in page.
However, when accessing the download page the "#download" selector is not found because it is embedded. This selector is the download button.
Follow the code I’m using
const puppeteer = require ('puppeteer'); //biblioteca puppeteer
async function getdados () {
const inscricao = '015440380448440' //número da inscrição consultada
const browser = await puppeteer.launch({ headless: false }); //abre o browser
const page = await browser.newPage(); //abre nova aba
await page.goto('https://emissao.cuiaba.mt.gov.br/portal/'); //acessa a primeira página
await page.waitForSelector('body'); //aguarda carregar.
await page.waitForSelector('#form1'); //aguarda carregar.
await page.goto('https://emissao.cuiaba.mt.gov.br/portal/EmissaoGuia.aspx'); //acessa a segunda página
await page.type('#ctl00_ContentPlaceHolder1_txt_inscricao', inscricao, {delay: 100}); //clica na barra e digita a inscrição
await page.click('#ctl00_ContentPlaceHolder1_BtnContinuar'); //clica no botão continuar
page.on('dialog', async dialog => {
await dialog.accept();
}); //aceita caixa de alerta se houver
await page.waitForSelector('#ctl00_ContentPlaceHolder1_ASPxGrdVwPadrao_cell0_0_chk'); //aguarda carregar a 1º caixa de guia.
await page.click('#ctl00_ContentPlaceHolder1_ASPxGrdVwPadrao_cell0_0_chk')//clica na 1º caixa de guia.
await page.click('#ctl00_ContentPlaceHolder1_BtnContinuar')//clica em gerar guia
await page.waitForSelector('body > embed'); //aguarda carregar a página da guia.
const url = page.url() //captiura a url atual
console.log(url) // verifica se a url refere-se a url da guia.
await page.click('body > embed'); //clica no corpo da página
const download = await page.evaluate(() => {
const uiElement = document.querySelector('#download');
return uiElement
}); //Procura o selector "#download"
await page.click(download); //clicka no botão de download.
};
getdados ()
To do the PDF saving action would a library be required for PDF manipulation? I say this because trying to use the Ctrl+S command did not work. This command would open the folder to save the file.
– Felipe Diniz