【问题标题】:Phantomjs reloading same page for every 5 secondsPhantomjs 每 5 秒重新加载同一页面
【发布时间】:2020-01-16 19:42:17
【问题描述】:

我希望我的 phantomjs 脚本每隔 5 秒对给定的参数输入域 https://google.com 执行重新加载/刷新。我如何做到这一点?

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);
            });
        }
    }); }
}

【问题讨论】:

    标签: javascript jquery phantomjs mocha-phantomjs


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2014-10-02
      • 2020-03-06
      • 2011-04-28
      • 2016-11-17
      • 1970-01-01
      • 2020-10-24
      • 2016-07-10
      • 2021-01-04
      • 2018-08-02
      相关资源
      最近更新 更多