javascript PhantomJS 一页一页地打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30954345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PhantomJS open one page after another
提问by macroscripts
I used this example to create a phantomjs code to login to website.
我用这个例子创建了一个 phantomjs 代码来登录网站。
var page = require('webpage').create();
page.open("http://www.facebook.com/login.php", function(status) {
if (status === "success") {
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
page.evaluate(function() {
console.log('hello');
document.getElementById("email").value = "email";
document.getElementById("pass").value = "password";
document.getElementById("u_0_1").click();
// page is redirecting.
});
setTimeout(function() {
page.evaluate(function() {
console.log('haha');
});
page.render("page.png");
phantom.exit();
}, 5000);
}
});
From this link. https://gist.github.com/ecin/2473860
从这个链接。 https://gist.github.com/ecin/2473860
But I want to open another link from a button or go directly on it. How can I do it?
但我想从按钮打开另一个链接或直接打开它。我该怎么做?
Here is a simpler example. Doesn't work...
这是一个更简单的例子。不起作用...
var page = require('webpage').create();
var url = "www.example.com";
page.open(url, function (status) {
setTimeout(function () {
page.evaluate(function () {
console.log('haha');
});
page.render("example.png");
phantom.exit();
}, 5000);
});
var url = "www.google.com";
page.open(url, function (status) {
setTimeout(function () {
page.evaluate(function () {
console.log('haha');
});
page.render("google.png");
phantom.exit();
}, 5000);
});
采纳答案by Artjom B.
Very close, now combine your two snippets into one. page.open()
is asynchronous which is why you need to open the next page only after the first one has finished:
非常接近,现在将您的两个片段合二为一。page.open()
是异步的,这就是为什么你需要在第一个完成后打开下一个页面:
var page = require('webpage').create();
var url = "http://www.example.com";
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("email").value = "email";
document.getElementById("pass").value = "password";
document.getElementById("u_0_1").click();
// page is redirecting.
});
setTimeout(function () {
page.evaluate(function () {
console.log('haha');
});
page.render("example.png");
var url = "http://www.google.com";
page.open(url, function (status) {
setTimeout(function () {
page.evaluate(function () {
console.log('haha');
});
page.render("google.png");
phantom.exit();
}, 5000);
});
}, 5000);
});
To actually see the console.log()
inside of page.evaluate()
you will need to register to the page.onConsoleMessage
event. There are more other events that are helpful when debugging.
要真正看到您的console.log()
内部,page.evaluate()
您需要注册该page.onConsoleMessage
活动。还有更多其他有助于调试的事件。
Don't forget to add the protocol (http:// or file:///) to the URLs that you're opening. PhantomJS is a bit picky in that regard.
不要忘记将协议(http:// 或 file:///)添加到您打开的 URL 中。PhantomJS 在这方面有点挑剔。
Instead of waiting a static amount of time (setTimeout()
) until the next page is loaded after you do some action. You should make use of the page.onLoadFinished
event. This is rather cumbersome to get right for navigation intensive scripts. Use CasperJSfor longer scripts.
而不是setTimeout()
在执行某些操作后等待一段静态时间 ( ) 直到加载下一页。你应该利用这个page.onLoadFinished
事件。这对于导航密集型脚本来说是相当麻烦的。对于较长的脚本,请使用CasperJS。
Oftentimes Element.click()
doesn't work. This questionhas many solutions for those cases.
很多时候Element.click()
是行不通的。对于这些情况,这个问题有很多解决方案。