jonhoo / fantoccini

A high-level API for programmatically interacting with web pages through WebDriver.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

excute() doesnt execute some scripts correctly

opened this issue · comments

So I was playing a lot with the execute function to try and get it working as expected but only with partial success unfortunately, I am using chromedriver + latest chrome build to goto to a certain website and run 2 scripts there which suppose to yield functions mounted on the window.

The first script which I run works okay and yields the expected variables on the window object, however, when I run the second script successfully, I should mention, I don't see any of the functions that are supposed to appear on the window.

I both tried execute and execute_async, the latter just hangs until the timeout threshold is exceeded.

I also tried running without the --headless arg to see everything in action. I do see the console.logs from both scripts in the console tab (dev tools) and I can see the source of each when I click on the origin of the console logs.

I do know that the second script is not buggy/broken because if I run it by hand in the console tab it mounts the required functions.

You can see the reproduction code here.

Thanks!

I also got the idea of trying to execute a script that will mount the scripts on the head of the web page I am accessing and let the browser do its thing.
So something like

let script = document.createElement('script');
script.onload = () => {
  // script src
}
document.head.appendChild(script);

Will update here if this solves the issue I am having.

UPD: It didn't work

Ok, I solved it by altering the script's code a little and pointing the relevant function to the window. Fatoccini works great thanks!

Edit: I am still not sure why it doesn't work the same as it would work if I would paste it in the Console tab, wonder if there is a reasonable explanation for this maybe on how the WebDriver executes the scripts it receives (out of the window context).

It would be a good idea to leave an explanation for future searchers on this thread before resolving it probably (If anyone has any explanation for this).

The spec for executing a script seems to suggest that it gets access to the window. If you spot any caveats or gotchas there though, happy to take PR to add a note to the docs!