I have written a PhantomJS script for a particular page and tested it. Now I am trying to replicate the same script to another website having a similar page but the results I am getting are quite different.
I am trying to click on a size of a product on this page and then click on the "Add to Cart" button. Here is my script for the same. Somehow, the size selection is not working.
Here is the code:
var system = require('system');
var page = require('webpage').create();
page.viewportSize = {
width: "1280",
height: "800"
};
var inputElements = {
"url": "http://www.jabong.com/tom-tailor-Navy-Blue-Casual-Shirt-1167065.html",
"size": "M"
}
page.open(inputElements.url, function (status) {
page.render("1_product_page.png");
var point = page.evaluate(function (sizeToSelect){
var sizes = document.querySelector(".size-desktop").querySelectorAll('li.first.popover-options');
var filter = Array.prototype.filter;
var selected = filter.call(sizes, function(size){ return size.textContent == sizeToSelect });
if(selected && selected.length){
selected = selected[0];
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, selected);
selected.dispatchEvent(evt);
} else {
return { "error": "size not available" }
}
}, inputElements.size)
console.log(JSON.stringify(point));
if(point && point.error){
console.log(JSON.stringify(point, null, 4));
phantom.exit(1);
}
setTimeout(function (){
page.render("2_size_selected.png");
console.log("size selected");
page.evaluate(function (){
var buyButton = document.querySelector(".btn.btn-payment.add-to-cart")
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, buyButton);
buyButton.dispatchEvent(evt);
})
}, 2000);
setTimeout(function (){
page.render("3_buy_button_clicked.png");
phantom.exit(0);
}, 6000)
});
|
I have written a PhantomJS script for a particular page and tested it. Now I am trying to replicate the same script to another website having a similar page but the results I am getting are quite different. I am trying to click on a size of a product on this page and then click on the "Add to Cart" button. Here is my script for the same. Somehow, the size selection is not working. Here is the code:
If I try to return |