同步打开PhantomJS中的链接

同步打开PhantomJS中的链接

本文介绍了同步打开PhantomJS中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有办法同步点击PhantomJS上的链接(打开链接)。首先必须打开第一页,然后单击第一页上的链接转到第二页。这是我使用setTimeout的方法:

I want to know is there any way to click link (open link) on PhantomJS synchronously. First page must be opened first, then click link on the first page to go to second page. Here is my approach using setTimeout:

var page = require('webpage').create();
var url = "http://domain.tld/index.html";

page.open(url, function (status) {
    page.render("1-home.png");

    page.evaluate(function() {
        // search for element and click it to redirect
        document.getElementById("RetailUser").click();
    });

    // I use setTimeout to wait for firstpage to get loaded
    setTimeout(function () {
        // do another process on second page
    }, 5000);

}

PS:我知道我可以使用CasperJS完成它。但如果可能的话,我不想使用CasperJS。

PS: I aware I can accomplish it using CasperJS. But if possible, I don't want to use CasperJS.

推荐答案

单击链接始终是同步的。之后发生的事情总是异步的,因为JavaScript的异步性质。

Clicking a link is always synchronously. What happens afterwards is always asynchronously, because of the asynchronous nature of JavaScript.

有多种方法可以等待下一页:

There are multiple ways to wait for the next page:


  • 静态金额时间与,

  • 动态w使用,

  • 通过在点击之前注册 page.onLoadFinished 加载下一页,

  • 等待所有未完成的请求完成(和)或

  • 这四种方法的组合。

  • Static amount of time with setTimeout,
  • Dynamic waiting for a condition with waitFor,
  • Next page load by registering to page.onLoadFinished before the click,
  • Wait until all outstanding requests are finished (one and two) or
  • A combination of those four methods.

这篇关于同步打开PhantomJS中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:22