简体   繁体   English

如何使用PhantomJS在单个网页中发送多次点击?

[英]How to send multiple clicks in a single web page using PhantomJS?

I'm using PhantomJS 2.1.1 to take screenshots of web pages. 我正在使用PhantomJS 2.1.1拍摄网页的屏幕截图。

What I need on this particular page is to click several times on a div element in order to slide pictures in a diaporama, by using the following code in a page.evaluate block : 此特定页面上,我需要在div元素上单击几次,以通过在page.evaluate块中使用以下代码来在透辉中滑动图片:

page.open(url, function () {
    page.evaluate(function() { 
        for (i = 0; i < 10; i++) {
            setTimeout(function(){
                document.querySelector("div.linksNavNext.linksNav.icon-fleche-suiv").click();
            }, 500);
        }
    });       

    setTimeout(function(){
        page.render('url.png');
        phantom.exit();
    }, 2000);
});

As you see I try to get the 10th picture in the diaporama by sending 10 times the click event. 如您所见,我尝试通过发送10次click事件来获取透散中的第十张图片。 But what I get is just the second picture. 但是我得到的只是第二张照片。 It seems the click event is sent only once, despite the for loop. 尽管有for循环,看来click事件仅发送一次。

What am I doing wrong? 我究竟做错了什么?

It seems all 10 of your clicks are executed in just 500 ms. 似乎所有10次点击都在500毫秒内执行。 You need to postpone timeout: 您需要推迟超时:

for (i = 0; i < 10; i++) {
  setTimeout(function(){
    document.querySelector("div.linksNavNext").click();
  }, 500 * i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM