The element is clicked, but Page is not reloaded accordingly

人盡茶涼 提交于 2019-12-25 03:52:40

问题


Using phantom JS, I am trying to click a <li> element(target_element) and render the page which loads after it. The element is clicked successfully. But, on rendering, the refreshed page is not seen in the output. I have given enough wait item. The code is:

page.open(address, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
        phantom.exit();
    } else {
        window.setTimeout(function () {
            page.evaluate(function() {
                document.getElementById('custom_filters').style.visibility = "hidden";
                var a = document.getElementById('target_element');
                var e = document.createEvent('MouseEvents');
                e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                console.log(a.dispatchEvent(e));
                waitforload = true;
            });
            page.render(output);
            phantom.exit();
        }, waitTime);
    }
});

Does this mean The event has not triggered the JS function or am I doing anything wrong here?


回答1:


No, you didn't have any wait time between click and render. The setTimeout needs to be only around the calls that need to be delayed.

page.evaluate(function() {
    document.getElementById('custom_filters').style.visibility = "hidden";
    var a = document.getElementById('target_element');
    var e = document.createEvent('MouseEvents');
    e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    console.log(a.dispatchEvent(e));
    waitforload = true;
});
window.setTimeout(function () {
    page.render(output);
    phantom.exit();
}, waitTime);


来源:https://stackoverflow.com/questions/25357122/the-element-is-clicked-but-page-is-not-reloaded-accordingly

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