Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer can't find selector

I'm attempting to do a bit of web scraping using Puppeteer, but the script seems unable to find the selector I'm looking for. Basically this code:

const puppeteer = require('puppeteer');

let scrape = async () => {
const year = 18;

const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('https://cobbcounty.org/index.php?option=com_wrapper&view=wrapper&Itemid=2008');
await page.waitFor(5000);
var id = '';
for(i=0;i<10000;i++){
    id = i;
    await page.click('#txtCase');
    await page.keyboard.type(year + '-P-' + id);
    await page.select('#lstDoc','Estate');
}
}

scrape().then((value) => {
console.log('script ended');
});

Is giving me this error:

(node:31125) UnhandledPromiseRejectionWarning: AssertionError 
[ERR_ASSERTION]: No node found for selector: #txtCase

As far as I can tell, #txtCase is an actual selector on the page, so I don't know why puppeteer can't find it. If someone can explain to me what I'm doing wrong it would be really helpful.

like image 679
Nicholas Jennings Avatar asked May 29 '18 16:05

Nicholas Jennings


People also ask

What is a selector puppeteer?

# Puppeteer and its approach to selectors. Puppeteer is a browser automation library for Node: it lets you control a browser using a simple and modern JavaScript API. The most prominent browser task is, of course, browsing web pages. Automating this task essentially amounts to automating interactions with the webpage.


1 Answers

As far as I can tell, #txtCase is an actual selector on the page, so I don't know why puppeteer can't find it.

Try loading the page and using the console to find that element.

document.querySelector('#txtCase')
null

It's not there. I know you can see it when you right-click to inspect that text field, but it's nested in an iframe. You need to access that frame, then find the button, then click on it.

const frame = await page.frames().find(f => f.name() === 'iframe');
const button = await frame.$('#txtCase');
button.click();
like image 109
posit labs Avatar answered Sep 24 '22 06:09

posit labs