运行 网站的 JavaScript 函数在 phantomjs 中
Running web-site's JavaScript functions inside phantomjs
我正在使用 phantomjs 访问某些网站。 page.evaluate 方法中的站点环境是否有可能 运行 功能?你能提供正确用法的例子吗?
是的,当然有可能。您只需在 page.evaluate
中调用站点函数即可。考虑这个例子:
example.com html
<html>
<head>
</head>
<body style="background-color: white">
<p>A page</p>
<script>
function makeRed() {
document.body.style.backgroundColor = "red";
}
</script>
</body>
</html>
PhantomJS 脚本
var page = require('webpage').create();
page.viewportSize = { width: 600, height: 300 };
page.open('http://example.com', function() {
page.evaluate(function(){
makeRed();
});
setTimeout(function(){
page.render('red.png');
phantom.exit();
}, 1000);
});
结果:
我正在使用 phantomjs 访问某些网站。 page.evaluate 方法中的站点环境是否有可能 运行 功能?你能提供正确用法的例子吗?
是的,当然有可能。您只需在 page.evaluate
中调用站点函数即可。考虑这个例子:
example.com html
<html>
<head>
</head>
<body style="background-color: white">
<p>A page</p>
<script>
function makeRed() {
document.body.style.backgroundColor = "red";
}
</script>
</body>
</html>
PhantomJS 脚本
var page = require('webpage').create();
page.viewportSize = { width: 600, height: 300 };
page.open('http://example.com', function() {
page.evaluate(function(){
makeRed();
});
setTimeout(function(){
page.render('red.png');
phantom.exit();
}, 1000);
});
结果: