how to wait for element visibility in phantomjs

后端 未结 4 1500
予麋鹿
予麋鹿 2020-11-29 02:58

Users click this link:


Now I\'m simulating the click i

4条回答
  •  天命终不由人
    2020-11-29 03:24

    Heres a spin of Cybermaxs's answer:

    function waitFor ($config) {
        $config._start = $config._start || new Date();
    
        if ($config.timeout && new Date - $config._start > $config.timeout) {
            if ($config.error) $config.error();
            if ($config.debug) console.log('timedout ' + (new Date - $config._start) + 'ms');
            return;
        }
    
        if ($config.check()) {
            if ($config.debug) console.log('success ' + (new Date - $config._start) + 'ms');
            return $config.success();
        }
    
        setTimeout(waitFor, $config.interval || 0, $config);
    }
    

    Example of use:

    waitFor({
        debug: true,  // optional
        interval: 0,  // optional
        timeout: 1000,  // optional
        check: function () {
            return page.evaluate(function() {
                return $('#thediv').is(':visible');
            });
        },
        success: function () {
            // we have what we want
        },
        error: function () {} // optional
    });
    

    It's a little easier when you use a config variable.

提交回复
热议问题