gpt4 book ai didi

javascript - PhantomJS 一页一页打开

转载 作者:可可西里 更新时间:2023-11-01 02:41:06 24 4
gpt4 key购买 nike

我用这个例子创建了一个 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);
}
});

从这个链接。 https://gist.github.com/ecin/2473860

但我想从一个按钮打开另一个链接或直接进入它。我该怎么做?

这是一个更简单的例子。没用...

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);

});

最佳答案

非常接近,现在将您的两个片段合并为一个。 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);
});

要实际查看 page.evaluate() 中的 console.log(),您需要注册 page.onConsoleMessage事件。还有更多其他事件对调试有帮助。

不要忘记将协议(protocol)(http://或 file:///)添加到您打开的 URL。 PhantomJS 在这方面有点挑剔。

而不是在执行某些操作后等待一段静态时间 (setTimeout()) 直到加载下一页。您应该使用 page.onLoadFinished 事件。这对于导航密集型脚本来说是相当麻烦的。使用 CasperJS对于更长的脚本。

Element.click() 通常不起作用。 This question对于这些情况有很多解决方案。

关于javascript - PhantomJS 一页一页打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30954345/

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