@@ -419,6 +419,51 @@ 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: polling_interval shall not be too small
429
+ local ok, result1, result2 = splash:with_timeout(function()
430
+ while true 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
+ end
440
+ end, timeout)
441
+ if not ok then
442
+ return nil, string.format('timeout exceeded: %s', result1)
443
+ end
444
+ return result1, result2
445
+ end
446
+
447
+ function main(splash)
448
+ -- Goto example.com and wait for a specific node to be loaded in the DOM
449
+ splash:go('http://example.com')
450
+ wait_until(splash, 10, 0.1, splash.select, splash, 'div#example_selector')
451
+
452
+ -- Goto example.com and wait for a specific response to be downloaded
453
+ local example_response_downloaded = false
454
+ splash:on_response(function (response)
455
+ if string.find(response.url, 'specific_url_pattern_here') then
456
+ example_response_downloaded = true
457
+ end
458
+ end)
459
+ splash:go('http://example.com')
460
+ wait_until(
461
+ splash, 10, 0.1,
462
+ function ()
463
+ return example_response_downloaded
464
+ end
465
+ )
466
+ end
422
467
423
468
.. _splash-jsfunc :
424
469
0 commit comments