ulixee / hero

The web browser built for scraping
MIT License
652 stars 32 forks source link

plugins/execute-js does not execute certain JS methods #154

Closed massafilippo97 closed 1 year ago

massafilippo97 commented 1 year ago

I was trying to execute some JS code thanks to the execute-js plugin, but I've encountered a problem regarding executing methods like querySelector() or just referencing window.document.head or .body (they will always return a null value).

  const box = window.document.createElement('element1');
  console.log(box) //creation of new html element is  successful!
  const styleElement = window.document.createElement('style');
  styleElement.innerHTML = " CSS code here";

  document.head.appendChild(styleElement); //head === null
  document.body.appendChild(box);` //body === null

  console.log(document.querySelector("head")) //null
  console.log(document.querySelector("p")) //null
  console.log(window.document) // !== null

The returned error message is something like this:

TypeError: Cannot read properties of null (reading 'appendChild')
    at myFunction (<anonymous>:47:33)
    at <anonymous>:71:5
blakebyrnes commented 1 year ago

Could you share what you’re running before this code? Id like to see how you’re doing your goto and if you’re waiting for any load events before running your execute js. It might be running too fast, before the dom has loaded. You can do a waitForLoad(‘DomContentLoaded’) before your executeJS(‘…snippet below’)

On Oct 5, 2022, at 6:40 AM, Filippo Massa @.***> wrote:

 I was trying to execute some JS code thanks to the execute-js plugin, but I've encountered a problem regarding executing methods like querySelector() or just referencing window.document.head or .body (they will always return a null value).

const box = window.document.createElement('element1'); console.log(box) //creation of new html element is successful! const styleElement = window.document.createElement('style'); styleElement.innerHTML = " CSS code here";

document.head.appendChild(styleElement); //head === null document.body.appendChild(box);` //body === null

console.log(document.querySelector("head")) //null console.log(document.querySelector("p")) //null console.log(window.document) // !== null The returned error message is something like this:

TypeError: Cannot read properties of null (reading 'appendChild') at myFunction (:47:33) at :71:5 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.

massafilippo97 commented 1 year ago

Okay, my bad. What I was trying to do was surfing to the website with hero.goto("url"), then immediately executing JS code without waiting the dom to be loaded.

 await hero.goto(url);
 //await hero.waitForLoad("DomContentLoaded") //what I needed to make it work
 await hero.executeJs(myFunction)

Thanks for the help!