相关疑难解决方法(0)

将参数传递给page.includeJs()和page.evaluate()中的匿名函数

一点背景......我对javascript和phantom.js有点新,所以我不知道这是一个javascript还是phantom.js bug(功能?).

以下成功完成(对于缺少的phantom.exit(),您只需要在完成后按ctrl + c):

var page = require('webpage').create();
var comment = "Hello World";

page.viewportSize = { width: 800, height: 600 };
page.open("http://www.google.com", function (status) { 
    if (status !== 'success') {
        console.log('Unable to load the address!');
        phantom.exit();
    } else {
        page.includeJs('http://code.jquery.com/jquery-latest.min.js', function() {
            console.log("1: ", comment);
        }, comment);

        var foo = page.evaluate(function() {            
            return arguments[0];
        }, comment);

        console.log("2: ", foo);            
    }
});
Run Code Online (Sandbox Code Playgroud)

这有效:

page.includeJs('http://code.jquery.com/jquery-latest.min.js', function() {
    console.log("1: ", comment);
}, comment);
Run Code Online (Sandbox Code Playgroud)

输出:1: Hello World

但不是:

page.includeJs('http://code.jquery.com/jquery-latest.min.js', function(c) { …
Run Code Online (Sandbox Code Playgroud)

javascript phantomjs

17
推荐指数
1
解决办法
8565
查看次数

PhantomJS不接受传递给page.evaluate()的函数的参数

我无法传递参数传递给函数page.evaluate.我想提交表格.

data1 = "Textsample";
page.evaluate(function(data1) {
    var form = document.getElementById ("MyForm");
    form.data.value = data1;
    form.submit();
});
Run Code Online (Sandbox Code Playgroud)

但是当我截取页面截图时,数据字段填充了"未定义".我做错了什么以及如何解决?

javascript arguments phantomjs

7
推荐指数
1
解决办法
1851
查看次数

标签 统计

javascript ×2

phantomjs ×2

arguments ×1