Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node-phantom is not enabling javascript on web pages

I am using node-phantom to post items to a cart on a web site. The problem is that when I use page.open to navigate to the shopping cart page (after already having added an item to the cart), I get an html response saying that I need to enable javascript in my browser in order to view the shopping cart page. I've checked the settings.javascriptEnabled setting and found it to be set to 'true'. At this point I am confused, why does the page think that phantomjs does not have javascript enabled?

Here is my code:

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);
                });
            });
        });
    });
});
like image 295
bbeny Avatar asked Nov 10 '22 16:11

bbeny


1 Answers

Replace

page.injectJs()

with

page.includeJs()

UPDATE

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

            });
        });
    })
});
like image 193
dcodesmith Avatar answered Nov 14 '22 21:11

dcodesmith