gpt4 book ai didi

phantomjs - 将变量传递到page.evaluate-PhantomJS

转载 作者:行者123 更新时间:2023-12-03 11:31:19 31 4
gpt4 key购买 nike

是否可以在page.evaluate中传递变量?

function myFunction(webpage, arg1, arg2){

var page = require('webpage').create();

page.viewportSize = { width: 1920, height: 1080 };

page.open(webpage, function (status){

if (status == 'success') {

page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js", function(){

page.evaluate(function(){

arg = arg1 + arg2;
console.log(arg);

});

});

}

else { phantom.exit(); }

});

}

我尝试了几种在Internet上找到的方法,但实际上没有什么方法无法获得其变量。

预先感谢您的帮助 :)

最佳答案

像往常一样,在evaluate函数的documentation中明确说明了答案:

As of PhantomJS 1.6, JSON-serializable arguments can be passed to the function. In the following example, the text value of a DOM element is extracted. The following example achieves the same end goal as the previous example but the element is chosen based on a selector which is passed to the evaluate call:



以下示例演示了用法:
var title = page.evaluate(function(s) {
return document.querySelector(s).innerText;
}, 'title');
console.log(title);

关于phantomjs - 将变量传递到page.evaluate-PhantomJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356219/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com