Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to wait for page loads #606

Closed
jambonnade opened this issue May 10, 2017 · 3 comments
Closed

Question: how to wait for page loads #606

jambonnade opened this issue May 10, 2017 · 3 comments

Comments

@jambonnade
Copy link

jambonnade commented May 10, 2017

Hi,

First, i'd like to know exactly when the splash:go() call returns :

  • is it when the DOM is ready (like the jQuery() function) ?
  • is it when everything is loaded (like window.onload) ?
  • earlier the two above ?

Then how should we deal with scripts going through multiple pages without additional go() calls ? (ex : click on links, form submits)
Using wait() with cancel_on_redirect flag is a good start but again i don't know when wait() returns exactly in this case.

I don't find it's a serious way to add wait() calls with random timings to let the page finish loading, so if there is no designed way for this, i may do something like : check at some interval that the page has a specific html element or if there is a javascript variable indicating that the new page is loaded

Thanks

@ghost
Copy link

ghost commented May 30, 2017

@jambonnade You may refer to this sample wait for a specific element to load script.

function wait_for_element(splash, css, maxwait)
-- Wait until a selector matches an element
-- in the page. Return an error if waited more
-- than maxwait seconds.
if maxwait == nil then
maxwait = 10
end
return splash:wait_for_resume(string.format([[
function main(splash) {
var selector = '%s';
var maxwait = %s;
var end = Date.now() + maxwait*1000;

  function check() {
    if(document.querySelector(selector)) {
      splash.resume('Element found');
    } else if(Date.now() >= end) {
      var err = 'Timeout waiting for element';
      splash.error(err + " " + selector);
    } else {
      setTimeout(check, 200);
    }
  }
  check();
}

]], css, maxwait))
end

function main(splash)
splash:go("http://scrapinghub.com")
wait_for_element(splash, "#foo")
return {png=splash:png()}
end

@dalepo
Copy link

dalepo commented Apr 21, 2018

The above example works but it has a problem. If the page reloads, it interrupts the script execution. So I wrote a function purely in lua to handle this kind of problem.

function wait_for_element(splash, css, maxwait)
    if maxwait == nil then
        maxwait = 10
    end
    local exit = false
    local time_chunk = 0.2
    local time_passed = 0
    while (exit == false)
    do
        local element = splash:select(css)
        if element then
            exit = true
        elseif time_passed >= maxwait then
            exit = true
            error('Timed out waiting for -' .. css)
        else
            splash:wait(time_chunk)
            time_passed = time_passed + time_chunk
        end
    end
end

@dalepo
Copy link

dalepo commented May 2, 2018

Apparently the above function has high cpu usage, not sure why. I don't recommend using it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants