【发布时间】:2013-03-27 12:02:33
【问题描述】:
我正在使用 PhantomJS 填写并提交表单,然后输出结果页面。问题是,我根本不知道这件事是否正在提交。
我打印结果页面,但它与原始页面相同。我不知道这是因为它重定向回来还是我没有提交它或者我需要等待更长时间或或或。在真正的浏览器中,它会发送一个 GET 并接收一个 cookie,在最终接收到最终结果 - 航班数据之前,它会使用该 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 一起使用,使其更加友好。
-
我想问题是我不确定这个页面是否能正常工作。就像他们正在积极阻止抓取尝试一样。 PhantomJs 是我正在尝试的第四件事。
-
使用 Casper,在操作之间暂停大约 400 毫秒,将用户代理更改为匿名的,例如'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/536.28.10 (KHTML, like Gecko) Version/6.0.3 Safari/536.28.10' (像 Phantom 一样在 Webkit 上,但与任何 Safari 用户相同在 OSX 10.8.3 上),如果这不起作用会感到惊讶。
-
让我困惑的事情(或让我困惑的一件事)是动作之间的等待。我是否需要为每个加载的页面/ajax 调用执行一个操作,或者它就像一个真正的浏览器,我在其中提交表单并执行其他所有操作?例如,网站在显示实际数据之前显示“等待”页面。
-
我只是在等待,因为我可能会用它来检查它是否是机器人。不过,您所描述的内容是必要的- casper 有一个名为 waitForSelector 的好东西:casperjs.org/api.html#casper.waitForSelector 它只允许您在匹配选择器时继续,所以应该可以为您解决这个问题。
标签: phantomjs