How to wait for page loading when using casperjs?

后端 未结 7 1199
名媛妹妹
名媛妹妹 2020-12-14 17:29

I am trying to scrape a webpage which has a form with many dropdowns and values in the form are interdependent. At many point I need the code to wait till the refresh of the

7条回答
  •  悲哀的现实
    2020-12-14 17:46

    I found this question when searching for solution to a problem where click() or fill() action reloads exactly the same data in a child iframe. Here is my improvement to Pebbl answer:

    casper.clickAndUnload = function (click_selector, unload_selector, callback, timeout) {
        var classname = 'reload-' + (new Date().getTime());
        this.evaluate(function (unload_selector, classname) {
            $(unload_selector).addClass(classname);
        }, unload_selector, classname);
    
        this.thenClick(click_selector);
        this.waitWhileSelector(unload_selector + '.' + classname, callback, timeout);
    };
    
    casper.fillAndUnload = function (form_selector, data, unload_selector, callback, timeout) {
        var classname = 'reload-' + (new Date().getTime());
        this.evaluate(function (unload_selector, classname) {
            $(unload_selector).addClass(classname);
        }, unload_selector, classname);
        this.fill(form_selector, data, true);
        this.waitWhileSelector(unload_selector + '.' + classname, callback, timeout);
    };
    

    This solution assumes that page uses jQuery. It should not be hard to modify it for pages that don't. unload_selector is an element that is expected to be reloaded after click or form submission.

提交回复
热议问题