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 https://bugzilla.mozilla.org/show_bug.cgi?id=1872254
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 #browser. Full source code & explanation is in this gist:
⁽¹⁾ https://blog.logrocket.com/intercepting-javascript-fetch-api-requests-responses/
⁽²⁾ https://github.com/chrome-php/
⁽³⁾ https://www.selenium.dev/documentation/webdriver/bidirectional/webdriver_bidi/script/
Terengganu sepatutnya dapat menang
21 Mei 2024
Penjaring gol pertama musim ini, Syafiq Ahmad
15 Mei 2024