admin管理员组

文章数量:1327661

I am trying to use puppeteer to check if a class exists on a webpage. For example let us just say you wanted to scrape certain data and you knew the data was being stored in a certain class. To grab the data you need to use the classname to grab it. Here is the code I am trying to use. It does not work.

        let pageClicked = document.querySelector('.classIAmTryingToFind')

        if(pageClicked){
            console.log('False')
            await browser.close() 
        }else{
            console.log('True')
            await browser.close() 
        }

I get this error when I run the code.

UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed.

I am trying to use puppeteer to check if a class exists on a webpage. For example let us just say you wanted to scrape certain data and you knew the data was being stored in a certain class. To grab the data you need to use the classname to grab it. Here is the code I am trying to use. It does not work.

        let pageClicked = document.querySelector('.classIAmTryingToFind')

        if(pageClicked){
            console.log('False')
            await browser.close() 
        }else{
            console.log('True')
            await browser.close() 
        }

I get this error when I run the code.

UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed.
Share Improve this question edited Mar 19, 2021 at 21:52 DisappointedByUnaccountableMod 6,8264 gold badges20 silver badges23 bronze badges asked Apr 24, 2020 at 3:27 Neik0Neik0 1033 silver badges9 bronze badges 3
  • github./puppeteer/puppeteer/issues/1175 "In my case, it was because I put await browser.close(); before await page.close();" – ariel Commented Apr 24, 2020 at 3:33
  • what. That is not my question. I want to be able to check if a class exists on a page. – Neik0 Commented Apr 24, 2020 at 3:49
  • if(pageClicked){ should work once you fix what's not your question – ariel Commented Apr 24, 2020 at 3:50
Add a ment  | 

2 Answers 2

Reset to default 7

I am not sure where or how you are executing your sample code. If we assume it is inside an 'evaluate' function's callback, this should work:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()

  await page.goto('https://stackoverflow./', { waitUntil: 'networkidle0' }) // check networkidle0 parameter and others here: https://pptr.dev/#?product=Puppeteer&version=v2.1.1&show=api-pagegotourl-options
  const pageClicked = await page.evaluate(() => {
    return !!document.querySelector('.classIAmTryingToFind') // !! converts anything to boolean
  })
  if (pageClicked) { // you had the condition reversed. Not sure if it was intended.
    console.log('True')
  } else {
    console.log('False')
  }
  await browser.close()
})()

I hope it helps!

You can use page.evaluate() to execute methods that are available in browser mode, like the one in the approved answer.

There is one another way. You can use page.$(".class_name") which returns null if the class is not found but returns a Promise that resolves to a NodeHandle of the selector on which you can execute other eligible puppeteer methods.

await page.$(".class_name") != null // means the class exists, class name should be prefixed with a .

you can also add the other selectors to it if you know if the data being read is in a table inside the class hierarchy

await page.$(".classname table") // this returns the selector of the table inside the class that you are looking for.

There are few more ways you can achieve this.

However, if you have lot of browser code that you have to write, I suggest you stay in the browser mode using page.evaluate() and write all the browser script inside it so that your code doesnt have to shift between node mode and browser mode. Each time page.method is executed, your process goes to browser mode and executes the relevant code in browser mode and returns to node mode again. To save these trips, you can choose to put a lot of relevant code into page.evaluate() which executes the entire code in browser mode and then returns node mode only once.

本文标签: javascriptPuppeteer How to check if class exists on a pageStack Overflow