There is a slightly better way to test this, but you need a wait loop nonetheless. The idea is to use splash:on_response(response) as a callback when refreshing the page. Note that the response callback will be called by async, so the main loop should wait for all page changes, so we have a wait loop (for example, given by @Krishnaraj).
Below is an example of pressing the button button_id 10 times to download additional content.
function main(splash) assert(splash:go(splash.args.url)) function wait_for(splash, condition) while not condition() do splash:wait(0.2) end end local clicks = 0 splash:on_response(function(res) clicks = clicks + 1 if clicks < 10 then assert(splash:runjs("document.getElementById(\"button_id\").click();")) end end) assert(splash:runjs("document.getElementById(\"button_id\").click();")) wait_for(splash, function() return clicks >= 10 end) return splash:html() end
source share