How to retrieve the ajax data whose loading requires a mouse click with PhantomJS or other tools

这一生的挚爱 提交于 2019-12-08 09:08:38

Tested this code, and it outputs the correct image with the desired tab selected. It wasn't so straightforward because of the underlying structure of the page. Hopefully you can use this as a bit of a learning exercise in processing the DOM.


// utility function to send mouseclick event to an element
function mouseclick( element ) {
    // create a mouse click event
    var event = document.createEvent( 'MouseEvents' );
    event.initMouseEvent( 'click', true, true, window, 1, 0, 0 );

    // send click to element
    element.dispatchEvent( event );
}

// final function called, output screenshot, exit
function after_clicked( page ) {
    console.log( "+after_clicked()" );

    page.render( "after_click.png" );
    console.log( "Done" );
    phantom.exit( 0 );
}

// middle function, click on desired tab
function click_div( page ) {
    console.log( "+click_div()" );

    var clicked = page.evaluate(
        function ( mouseclick_fn ) {
            // want the div with class "submenu"
            var div = document.querySelector( "div.submenu" );
            if ( ! div ) {
                return false;
            }

            // want all the list elements in the div
            var li_array = div.querySelectorAll( "li" );
            if ( ! li_array ) {
                return false;
            }

            // we want the 2nd list element
            var li2 = li_array[1];
            if ( ! li2 ) {
                return false;
            }

            // want the anchor inside the 2nd list element
            var anchor = li2.querySelector( "a" );
            if ( ! anchor ) {
                return false;
            }

            // must focus on anchor to trigger underlying javascript on page
            anchor.focus();

            // want the div within this anchor, so we can click on the div
            var element = anchor.querySelector( "div" );
            if ( ! element ) {
                return false;
            }

            // click on this inner div
            mouseclick_fn( element );
            return true;
        }, mouseclick
    );

    if ( ! clicked ) {
        console.log( "Failed to find desired element" );
        phantom.exit( 1 );
        return;
    }

    console.log( "- clicked, waiting 5 seconds" );
    window.setTimeout(
        function () {
            after_clicked( page );
        },
        5000
    );
}

// first function, create page, load page
function main() {
    console.log( "+main()" );

    var page = require('webpage').create();

    page.open(
        "http://sa.ttu.edu.tw/bin/home.php",
        function (status) {
            if ( status !== 'success' ) {
                console.log( "Failed" );
                phantom.exit( 1 );
                return;
            }

            console.log( "- page loaded, waiting 2 seconds..." );
            window.setTimeout(
                function () {
                    click_div( page );
                },
                2000
            );
        }
    );
}

main();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!