我使用PhantomJS填寫並提交表格,然後輸出生成的頁面。事情是,我不知道這件事是否被提交。PhantomJS:提交表格
我打印生成的頁面,但它與原始頁面相同。我不知道這是因爲它重新導向還是我沒有提交,或者我需要等待更長的時間或或。在真正的瀏覽器中,它發送一個GET並接收一個cookie,它在最終收到最終結果 - 飛行數據之前發送更多的GETS。
我複製了這個例子How to submit a form using PhantomJS,使用不同的url和page.evaluate函數。
var page = new WebPage(), testindex = 0, loadInProgress = false;
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
var steps = [
function() {
//Load Login Page
page.open("http://www.klm.com/travel/dk_da/index.htm");
},
function() {
//Enter Credentials
page.evaluate(function() {
$("#ebt-origin-place").val("CPH");
$("#ebt-destination-place").val("CDG");
$("#ebt-departure-date").val("1/5/2013");
$("#ebt-return-date").val("10/5/2013");
});
},
function() {
//Login
page.evaluate(function() {
$('#ebt-flightsearch-submit').click() ;
# also tried:
# $('#ebt-flight-searchform').submit();
});
},
function() {
// Output content of page to stdout after form has been submitted
page.evaluate(function() {
console.log(document.querySelectorAll('html')[0].outerHTML);
});
}
];
interval = setInterval(function() {
if (!loadInProgress && typeof steps[testindex] == "function") {
console.log("step " + (testindex + 1));
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] != "function") {
console.log("test complete!");
phantom.exit();
}
}, 50);
您可能想嘗試CasperJS - 它可以與Phantom一起使用,使其更友好。 – 2013-03-27 12:05:33
我想事情是,我不確定任何事情都可以用這個頁面。就像他們正在積極阻止拼搶企圖一樣。 PhantomJs是我嘗試的第四件事。 – user984003 2013-03-27 12:08:21
使用Casper,在動作之間暫停大約400ms,將User Agent更改爲匿名的例如'Mozilla/5.0(Macintosh; Intel Mac OS X 10_8_3)AppleWebKit/536.28.10(KHTML,如Gecko)Version/6.0.3 Safari/536.28.10'(這就像Phantom一樣的Webkit,但與任何Safari用戶相同在OSX 10.8.3上),如果不起作用,會感到驚訝。 – 2013-03-27 13:14:19