0

I am building a puppeteer script which accesses my browser extensions and goes to that page. Clicks on a specific extension button and fills in an input. However, I get this error:

throw new Error('Evaluation failed: ' + (0, util_js_1.getExceptionMessage)(exceptionDetails));
              ^
Error: Evaluation failed: TypeError: time is not a function
    at pptr://__puppeteer_evaluation_script__:10:8
    at ExecutionContext._ExecutionContext_evaluate (/Users/usr/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:229:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async ExecutionContext.evaluate (/Users/usr/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:107:16)
    at async Agent.getPage (/Users/usr/javascript/pending/request_3.js:25:21)

and the browser closes immediately right after. I have searched up similar issues which mention to not forget parentheses around the function newPage, or await, however I have these added and the issue still persists.

const puppeteer = require('puppeteer');

const EXTENSION = '/Users/usr/Library/Application Support/Google/Chrome/Profile 1/Extensions/gidnphnamcemailggkemcgclnjeeokaa/1.14.4_0';

class Agent {
  constructor(extension){
    this._extension = extension
  }

  async runBrowser(){
    const browser = await puppeteer.launch({
        headless: false,
        devtools: true,
        args: [
          `--disable-extensions-except=${this._extension}`,
          `--load-extension=${this._extension}`,
          '--enable-automation'
        ]
      })
      return browser }

    async getPage(textit){
      const page = await (await this.runBrowser()).newPage();
      await page.goto('chrome-extension://gidnphnamcemailggkemcgclnjeeokaa/popup.html');
      const event = await page.evaluate(async () => {
        document.getElementById('launch-trace').click()

        const someFunc = () => {
        const input = document.getElementById('user-trace-id');
        input.focus()
        input.value=textit
      }
       const time = setTimeout(someFunc, "3000")
       await time()
      })
      await page.close()
    }
}

const test = new Agent(EXTENSION);

test.getPage('bill')


me.limes
  • 441
  • 1
  • 13
  • Generally, sleeping is [considered bad](https://serpapi.com/blog/puppeteer-antipatterns/#overusing-waitfortimeout). That's why it was [removed from the Puppeteer API](https://stackoverflow.com/a/73676564/6243352). So although the dupe shows how to do this, 99.9% of the time, it's not what you want to do anyway. Try to find a selector or event to block on rather than a random guess at how long something might take. – ggorlen Sep 28 '22 at 18:12
  • @ggorlen That is a good point, I have added `waitforNavigation` instead however this eventually `timeouts` after 30 seconds, and `someFunc` does not activate. Although this is very much the best option if I can get it right. – me.limes Sep 28 '22 at 19:26

2 Answers2

1

here:

const time = setTimeout(someFunc, "3000")

return value of setTimeout() is positive integer not a function hence the type Error time is not a function.

setTimout documentation

webduvet
  • 4,220
  • 2
  • 28
  • 39
0

Because time is not a function.

You probably wanted to create a new function:

const time = () => new Promise(r => setTImeout(r, 3000))
await time()
Konrad
  • 21,590
  • 4
  • 28
  • 64