Chromeless - wait before executing instructions

536 views Asked by At

I am using Chromeless to retrieve a piece of information on a website and load a corresponding file:

async function run() {
  const chromeless = new Chromeless()

      const screenshot = await chromeless
        .goto('http://www.website.com')
         title = await chromeless.inputValue('input[name="title"]')

         var fs = require('fs');
         var data = fs.readFileSync(title,"utf8");
         ...
    await chromeless.end()
}

but the file read instructions are executed immediately when I launch the script and do not wait for the web crawling to be finished.

In javascript I think I would need to use callback functions to prevent that but is there a better way to do this with Chromeless?

2

There are 2 answers

2
grizzthedj On BEST ANSWER

You can try passing implicitWait: true to the Chromeless constructor. This value is false by default. Setting this to true will make Chromeless wait for elements to exist before executing commands.

In other words, var fs = require('fs'); shouldn't get executed until const title is assigned.

async function run() {
  const chromeless = new Chromeless({implicitWait: true})
  const screenshot = await chromeless.goto('http://www.website.com')
  const title = await chromeless.inputValue('input[name="title"]')

  var fs = require('fs');
  var data = fs.readFileSync(title,"utf8");
  ...
  await chromeless.end()
}
0
Ponyisasquare On

I you are trying to execute code a certain time in advance or upon an event then callbacks are the preferred way to do this. But JS is an asynchronous language. Honestly I don't use it much, but it seems like you could just make any type of async call and you wouldn't be blocked by the code anymore. Just breakout a function from your job that runs the blocking line, and it should run next to the current process. Possibly adding a sleep to ensure it's not going to keep your other code from running.