我正在使用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);
                });
            });
        });
    });
});

最佳答案

代替

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

            });
        });
    })
});

关于javascript - node-phantom没有在网页上启用javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20662418/

10-09 20:09