@@ -419,6 +419,48 @@ time after each page load in case of redirects:
419
419
return nil, "too_many_redirects"
420
420
end
421
421
422
+ It's also common practice to wait for a specific condition, other than a fixed
423
+ amount of time:
424
+
425
+ .. code-block :: lua
426
+
427
+ function wait_until(splash, timeout, polling_interval, check_func, ...)
428
+ -- XXX: Assuming the check function is fast enouch, as the time is not being counted.
429
+ local total_waited = 0
430
+ while total_waited < timeout do
431
+ local ok, reason = splash:wait(polling_interval)
432
+ if not ok then
433
+ return ok, string.format('wait failed: %s', reason)
434
+ end
435
+ local check_result = check_func(...)
436
+ if check_result then
437
+ return true, check_result
438
+ end
439
+ total_waited = total_waited + polling_interval
440
+ end
441
+ return nil, 'timeout exceeded'
442
+ end
443
+
444
+ function main(splash)
445
+ -- Goto example.com and wait for a specific node to be loaded in the DOM
446
+ splash:go('http://example.com')
447
+ wait_until(splash, 10, 0.1, splash.select, splash, 'div#example_selector')
448
+
449
+ -- Goto example.com and wait for a specific response to be downloaded
450
+ local example_response_downloaded = false
451
+ splash:on_response(function (response)
452
+ if string.find(response.url, 'specific_url_pattern_here') then
453
+ example_response_downloaded = true
454
+ end
455
+ end)
456
+ splash:go('http://example.com')
457
+ wait_until(
458
+ splash, 10, 0.1,
459
+ function ()
460
+ return example_response_downloaded
461
+ end
462
+ )
463
+ end
422
464
423
465
.. _splash-jsfunc :
424
466
0 commit comments