Tuesday 24 March 2020

Problem accessing the page while using puppeteer connection

I am trying to scrape information from an existing chrome page for learning purposes, using the puppeteer library. I have opened the chrome with debugging mode, received the ws url of the chrome page I am in, and connected successfully. Here is the connection code://open library const puppeteer = require('puppeteer'); //connect to excisting open page async function connectToChrome() { //Connect to excisting browser const wsChromeEndpointurl = 'ws://127.0.0.1:9222/devtools/page/5D6126455A889AA7D17B66C10729F352'; const browser = await puppeteer.connect ({ browserWSEndpoint: wsChromeEndpointurl }); The page which I have connected to is inside a url of a youtube video. My code's purpose is to find the amount of views of the video I am currently watching and log it to the console. When I have used this code while opening a new browser it was very easy, but now that I have connected to an existing page I don't really understand what is going on. I am not exactly sure how to access the information that is in the page, in a normal browser open you just open a new page, go to url, and save it to a variation. But right now I have no idea on which variation to call .$(xpath) to find the element//open library const puppeteer = require('puppeteer'); //connect to excisting open page async function connectToChrome() { //Connect to excisting browser const wsChromeEndpointurl = 'ws://127.0.0.1:9222/devtools/page/5D6126455A889AA7D17B66C10729F352'; const browser = await puppeteer.connect ({ browserWSEndpoint: wsChromeEndpointurl }); const [el] = await browser.page.$x('//*[@id="count"]/yt-view-count-renderer/span[1]'); const txt = await el.getProperty('textContent'); const txtJson = await txt.jsonValue(); console.log(txtJson); }; connectToChrome(); This code will return not understand the following line sentence, as it is undefined:browser.page.$x What would be the right syntax to access this element?

Submitted March 24, 2020 at 08:15PM by ProEngineerXD

No comments:

Post a Comment