Playlist MyTV untuk smart TV berserta intisari rancangan bermula RM4.99 sebulan. Selanjutnya →

How to preload script in NodeJS Selenium Webdriver

83CD50E8-D03D-4D60-A844-3973591F9738.jpeg

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 . 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/

Komentar (0):

FB: https://www.facebook.com/100810608155424/posts/1120303099136601/

Tulis komen: