const puppeteer = require("puppeteer"); (async function main() { try { const browser = await puppeteer.launch({headless: false}); const page = await browser.newPage(); page.setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"); await page.goto("https://www.qimai.cn/rank/index/brand/all/genre/6014/device/iphone/country/us/date/2019-03-19", {waitUntil: 'load', timeout: 0}); await page.waitForSelector(".container"); const sections = await page.$$(".container"); const freeButton = await page.$('[href="/rank/index/brand/free/device/iphone/country/us/genre/6014/date/2019-03-19"]'); await freeButton.click(); // free list const appTable = await page.waitForSelector(".data-table"); const lis = await page.$$(".data-table > tbody > tr > td"); // go to app content const appInfo = await page.$("a.icon"); // appInfo.click(); for (const content of lis) { const name = await content.$("div.appname"); const gameName = await page.evaluate(name => name.innerText, name); console.log("Game Name: ", gameName); } console.log("-- bingo --"); } catch (e) { console.log("our error", e); } })();
I cant seem to get the text from , and im getting this error: TypeError: Cannot read property 'innerHTML' of null. I have tried all ways, but its not working. This is the link to the website. https://www.qimai.cn/app/rank/appid/1451505313/country/us
Use the textContent property to get the text of an html element, e.g. const text = box. textContent . The textContent property returns the text content of the element and its descendants. If the element is empty, an empty string is returned.
page. $eval() function is used to get the value for an element in puppeteer. $eval will stage two-parameter as an argument first parameter will be the selector and the second parameter will be element= element.
textContents is all text contained by an element and all its children that are for formatting purposes only. innerText returns all text contained by an element and all its child elements. innerHtml returns all text, including html tags, that is contained by an element.
I use "waitForSelector" method and after that try to get the text
await page.waitForSelector('your selector') let element = await page.$('your selector') let value = await page.evaluate(el => el.textContent, element)
using waitForSelector
and evaluate
this becomes pretty clean
const element = await page.waitForSelector('your selector'); // select the element const value = await element.evaluate(el => el.textContent); // grab the textContent from the element, by evaluating this function in the browser context
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With