代码之家  ›  专栏  ›  技术社区  ›  Arbazz Hussain

Phantomjs每5秒重新加载同一页

  •  0
  • Arbazz Hussain  · 技术社区  · 7 年前

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

    幻影测试.js https://google.com

    • 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 回复  |  直到 7 年前
        1
  •  1
  •   Marcos Luis Delgado    7 年前

    你可以使用 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()