Jpa*_*ish 17 click pageload phantomjs
Phantomjs有这两个非常方便的回调onLoadStarted
,onLoadFinished
它允许你在页面加载时基本上暂停执行.但我一直在搜索,如果你click()
提交按钮或超链接,我找不到相应的.发生了类似的页面加载,但onLoadStarted
我没有调用此事件,因为没有明确的page.open()
发生.我正试图找出一种干净的方法来暂停执行此负载.
一个解决方案显然是嵌套的setTimeout,但我想避免这种情况,因为它是hacky并依赖于试验和错误而不是可靠和更强大的东西,如测试某事或等待事件.
我错过了这种页面加载的特定回调吗?或者也许有某种通用的代码模式可以处理这种事情?
编辑:
我还没弄明白如何让它暂停.这是onLoadStarted()
我调用click()
命令时不调用该函数的代码:
var loadInProgress = false;
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
page.open(loginPage.url, function (status) {
if (status !== 'success') {
console.log('Unable to access network');
fs.write(filePath + errorState, 1, 'w');
phantom.exit();
} else {
page.evaluate(function (loginPage, credentials) {
console.log('inside loginPage evaluate function...\n')
document.querySelector('input[id=' + loginPage.userId + ']').value = credentials.username;
document.querySelector('input[id=' + loginPage.passId + ']').value = credentials.password;
document.querySelector('input[id=' + loginPage.submitId + ']').click();
//var aTags = document.getElementsByTagName('a')
//aTags[1].click();
}, loginPage, credentials);
page.render(renderPath + 'postLogin.png');
console.log('rendered post-login');
Run Code Online (Sandbox Code Playgroud)
我仔细检查了id是否正确.在page.render()
将显示该信息被提交,但只有当我把它放在一个的setTimeout(),否则它会立即呈现它,我只看到凭证输入,页面重定向之前.也许我错过了别的什么?
Art*_* B. 13
我认为这些onLoadStarted
和onLoadFinished
功能都是你需要的.以下面的脚本为例:
var page = require('webpage').create();
page.onResourceReceived = function(response) {
if (response.stage !== "end") return;
console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + response.url);
};
page.onResourceRequested = function(requestData, networkRequest) {
console.log('Request (#' + requestData.id + '): ' + requestData.url);
};
page.onUrlChanged = function(targetUrl) {
console.log('New URL: ' + targetUrl);
};
page.onLoadFinished = function(status) {
console.log('Load Finished: ' + status);
};
page.onLoadStarted = function() {
console.log('Load Started');
};
page.onNavigationRequested = function(url, type, willNavigate, main) {
console.log('Trying to navigate to: ' + url);
};
page.open("http://example.com", function(status){
page.evaluate(function(){
// click
var e = document.createEvent('MouseEvents');
e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
document.querySelector("a").dispatchEvent(e);
});
setTimeout(function(){
phantom.exit();
}, 10000);
});
Run Code Online (Sandbox Code Playgroud)
它打印
Trying to navigate to: http://example.com/ Request (#1): http://example.com/ Load Started New URL: http://example.com/ Response (#1, stage "end"): http://example.com/ Load Finished: success Trying to navigate to: http://www.iana.org/domains/example Request (#2): http://www.iana.org/domains/example Load Started Trying to navigate to: http://www.iana.org/domains/reserved Request (#3): http://www.iana.org/domains/reserved Response (#2, stage "end"): http://www.iana.org/domains/example New URL: http://www.iana.org/domains/reserved Request (#4): http://www.iana.org/_css/2013.1/screen.css Request (#5): http://www.iana.org/_js/2013.1/jquery.js Request (#6): http://www.iana.org/_js/2013.1/iana.js Response (#3, stage "end"): http://www.iana.org/domains/reserved Response (#6, stage "end"): http://www.iana.org/_js/2013.1/iana.js Response (#4, stage "end"): http://www.iana.org/_css/2013.1/screen.css Response (#5, stage "end"): http://www.iana.org/_js/2013.1/jquery.js Request (#7): http://www.iana.org/_img/2013.1/iana-logo-header.svg Request (#8): http://www.iana.org/_img/2013.1/icann-logo.svg Response (#8, stage "end"): http://www.iana.org/_img/2013.1/icann-logo.svg Response (#7, stage "end"): http://www.iana.org/_img/2013.1/iana-logo-header.svg Request (#9): http://www.iana.org/_css/2013.1/print.css Response (#9, stage "end"): http://www.iana.org/_css/2013.1/print.css Load Finished: success
它显示单击链接会发出一次LoadStarted事件和两次NavigationRequested事件,因为存在重定向.诀窍是在执行操作之前添加事件处理程序:
var page = require('webpage').create();
page.open("http://example.com", function(status){
page.onLoadFinished = function(status) {
console.log('Load Finished: ' + status);
page.render("test37_next_page.png");
phantom.exit();
};
page.onLoadStarted = function() {
console.log('Load Started');
};
page.evaluate(function(){
var e = document.createEvent('MouseEvents');
e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
document.querySelector("a").dispatchEvent(e);
});
});
Run Code Online (Sandbox Code Playgroud)
如果您需要做这些事情,也许是时候尝试像CasperJS这样的东西.它运行在PhantomJS之上,但有一个更好的API用于浏览网页.
使用高级包装器,nightmarejs.你可以轻松地click
在那里等待.
这是代码(示例部分):
var Nightmare = require('nightmare');
new Nightmare()
.goto('http://yahoo.com')
.type('input[title="Search"]', 'github nightmare')
.click('.searchsubmit')
.run(function (err, nightmare) {
if (err) return console.log(err);
console.log('Done!');
});
Run Code Online (Sandbox Code Playgroud)
可以在github上找到更多示例和API用法
归档时间: |
|
查看次数: |
17475 次 |
最近记录: |