我正在使用node-phantom将项目发布到网站上的购物车中。问题是,当我使用page.open导航到购物车页面时(在已经将项目添加到购物车之后),我得到一个html响应,说我需要在浏览器中启用javascript才能查看购物购物车页面。我检查了settings.javascriptEnabled设置,发现它设置为'true'。此时我很困惑,为什么页面认为phantomjs没有启用javascript?
这是我的代码:
var phantom = require('node-phantom');
phantom.create(function (err, ph) {
ph.createPage(function (err, page) {
page.get('settings', function(err, oldSettings) {
console.log('\r\n oldSettings: ' + JSON.stringify(oldSettings));
page.open('http://www.somesite.com/shoppingcart/default.cfm', function (err, status) {
page.injectJs(jqueryPath, function (err) {
setTimeout(function() {
page.evaluate(function (injectedSku) {
var localErr;
var skuInCart;
var checkoutLnkMsg;
var pageHTML;
try {
pageHTML = $("html").html();
// Get 'SKUs' input element.
skuInCart = $('input[name="SKUs"]').val();
if (injectedSku === skuInCart) {
var checkoutLnk = $('#cartAction_bottom a[alt="Checkout"');
checkoutLnk.on("click", function() {
checkoutLnkMsg = '"' + checkoutLnk.href + '" link has been clicked';
});
checkoutLnk.click();
} else {
throw new Error('Product not in cart');
}
} catch (e) {
localErr = e;
}
return {
pageHTML: pageHTML,
err: localErr,
skuInCart: skuInCart,
checkoutLnkMsg: checkoutLnkMsg,
injectedSku: injectedSku
};
}, function (err, result) {
if (result.err) {
callback(err);
//return ph.exit();
}
fs.writeFileSync("./html_log.txt", result.pageHTML);
console.log('\r\n checkout - page.evaluate - injectedSku: ' + result.injectedSku);
console.log('\r\n checkout - page.evaluate - result.skuInCart: ' + JSON.stringify(result.skuInCart));
console.log('\r\n checkout - page.evaluate - result.checkoutLnkMsg: ' + result.checkoutLnkMsg);
callback(null);
//return ph.exit();
}, sku);
}, 1250);
});
});
});
});
});
答案 0 :(得分:1)
替换
page.injectJs()
与
page.includeJs()
<强>更新强>
var phantom = require('node-phantom');
phantom.create(function (err, ph) {
ph.createPage(function (err, page) {
page.get('settings', function(err, oldSettings) {
//console.log('\r\n oldSettings: ' + JSON.stringify(oldSettings));
page.open('http://www.footlocker.com/shoppingcart/default.cfm?', function (err, status) {
console.log(status);
var sku = 234; // assign sku id here
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js', function (err) {
setTimeout(function() {
page.evaluate(function (injectedSku) {
var localErr;
var skuInCart;
var checkoutLnkMsg;
var pageHTML;
try {
pageHTML = $("html").html();
// Get 'SKUs' input element.
skuInCart = $('input[name="SKUs"]').val();
if (injectedSku === skuInCart) {
var checkoutLnk = $('#cartAction_bottom a[alt="Checkout"');
checkoutLnk.on("click", function() {
checkoutLnkMsg = '"' + checkoutLnk.href + '" link has been clicked';
});
checkoutLnk.click();
} else {
throw new Error('Product not in cart');
}
} catch (e) {
localErr = e;
}
return {
pageHTML: pageHTML,
err: localErr,
skuInCart: skuInCart,
checkoutLnkMsg: checkoutLnkMsg,
injectedSku: injectedSku
};
}, function (err, result) {
if (result.err) {
// callback(err);
//return ph.exit();
}
// fs.writeFileSync("./html_log.txt", result.pageHTML);
console.log('\r\n checkout - page.evaluate - injectedSku: ' + result.injectedSku);
console.log('\r\n checkout - page.evaluate - result.skuInCart: ' + JSON.stringify(result.skuInCart));
console.log('\r\n checkout - page.evaluate - result.checkoutLnkMsg: ' + result.checkoutLnkMsg);
// callback(null);
ph.exit();
}, sku);
}, 1250);
});
});
});
})
});