Phantomjs 每 5 秒重新加载同一页面

Phantomjs reloading same page for every 5 seconds

我希望我的 phantomjs 脚本每 5 秒在给定的参数输入域 https://google.com 上继续执行 reloading/refreshing。我该如何实现?

phantomjs test.js https://google.com

var page = require('webpage').create(),
    system = require('system'),
    address;

page.onAlert = function (msg) {
    console.log("Received an alert: " + msg);
};

page.onConfirm = function (msg) {
    console.log("Received a confirm dialog: " + msg);
    return true;
};

if (system.args.length === 1) {
    console.log("Must provide the address of the webpage");
} else {
    address = system.args[1];
    for(var i=0; i <= 10; i++){
    page.open(address, function (status) {
        if (status === "success") {
            console.log("opened web page successfully!");
            page.evaluate(function () {
                var e = document.createEvent('Events');
                e.initEvent('click', true, false);
                document.getElementById("link").dispatchEvent(e);
            });
        }
    }); }
}

您可以使用 setTimeout 调用一个函数,该函数在页面加载一段时间后加载该页面:

var page = require('webpage').create(),
    system = require('system'),
    address;

page.onAlert = function (msg) {
    console.log("Received an alert: " + msg);
};

page.onConfirm = function (msg) {
    console.log("Received a confirm dialog: " + msg);
    return true;
};

function loadPage() {
  if (system.args.length === 1) {
    console.log("Must provide the address of the webpage");
  } else {
    address = system.args[1];
    page.open(address, function (status) {
      if (status === "success") {
        console.log("opened web page successfully!");
        page.evaluate(function () {
          var e = document.createEvent('Events');
          e.initEvent('click', true, false);
          document.getElementById("link").dispatchEvent(e);
        });
      }
      setTimeout(loadPage, 5000) // Call the function loadPage again in 5 seconds
    });
  }
}

loadPage()