Phantomjs每5秒刷新一次页面

时间:2019-09-15 23:22:36

标签: javascript jquery phantomjs mocha-phantomjs

我希望我的phantomjs脚本每隔https://google.com秒对给定的参数输入域5进行重新加载/刷新。我该如何实现?

  

phantomjs test.js https://google.com

  • test.js
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);
            });
        }
    }); }
}

1 个答案:

答案 0 :(得分:1)

您可以使用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()
相关问题