How to preload script in NodeJS Selenium Webdriver


Sabtu, 18 Mei 2024, 11:55 pm0

Preloading script on every page load allows you to do things like intercepting fetch() requests ⁽¹⁾.

I’ve done this before using Chrome PHP ⁽²⁾ addPreScript() function which utilizes Chrome Devtools Protocol (CDP) Page.addScriptToEvaluateOnNewDocument command.

In NodeJS selenium-webdriver, there’s a similar function which is driver.pinScript(), but I can’t get it working properly. Probably because I’m using geckodriver (Firefox) and there’s a warning message about CDP not properly supported:

Support for the Chrome DevTools Protocol (CDP) in Firefox will be deprecated after Firefox 128 (ESR) and will be removed in a later release. CDP users should consider migrating to WebDriver BiDi. See

It suggests to use BiDi instead of CDP, and I eventually found the documentation on Selenium website, under Script section ⁽³⁾.

Testing using the sample code is definitely working, however when I tried using a longer, more complex code, it suddenly not working anymore.

In the end I figure out a few tricks to ensure the script being pass into the preload function will work & you’ll be able to retrieve back the data from . Full source code & explanation is in this gist:


Komentar (0):


Tulis komen: