I'm trying to crawl the sizes for this product:
The problem: The sizes are loaded after the color of the product is selected.
In the product page's source code, I can see that the dropdown has a onchange-method: It clicks the form #postColor onchange.
The select dropdown:
<select name="color" id="color" class="cposelect" onchange="document.getElementById('postColor').click();" style="width:150px;margin-right: 20px; float: left;">
<option selected="selected" onfocus="if (this.storeCurrentControl != null) storeCurrentControl(event, this);" value="0">Select colour</option>
<option onfocus="if (this.storeCurrentControl != null) storeCurrentControl(event, this);" value="-8027">Light Camel</option>
<option onfocus="if (this.storeCurrentControl != null) storeCurrentControl(event, this);" value="-9999">black</option>
</select>
The #postColor form, which is clicked onchange:
<input type="submit" name="postColor" value="" onclick="location.href=('./?model=10344-4180&color='+document.forms[0].color.value+'&size='+document.forms[0].size.value+'&addbread=OUTLET&addbread2=DRIZIA&currentimage='+document.getElementById('currentimage').value+'&selectedmi=a1_INDEX_14&prev=10850-4314&next=10413-4183'); return false;" id="postColor" class="cpobutton " style="display: none;">
This is my code so far and it's not working:
casper.start('http://shop.baumundpferdgarten.com/showmodel/?model=10344-4180&addbread=OUTLET&addbread2=DRIZIA&color=0¤timage=1&selectedmi=a1_INDEX_14', function() {
this.test.assertExists('select[name="color"] option:nth-child(2)');
this.click('select[name="color"] option:nth-child(2)');
this.waitForSelector('select[name="size"] option:nth-child(2)', function() {
this.test.pass('selector is !');
var sizes = this.evaluate(function() {
console.log("======== evaluating ========");
// var sizes = document.querySelectorAll('#size option');
return document.querySelectorAll('#size option');
});
for (var i = sizes.length - 1; i >= 0; i--) {
console.log(sizes[i].innerText);
}
});
});
I suspect that problem is that a totally new page is loaded when a color is clicked (the sizes are not dynamically appended).
How would you solve this problem?
This is how I do it
The
change()
is very importantI'm assuming that you have jQuery on the page
The recommended jQuery solution doesn't actually work for me.
While, the
capture()
command shows the select option as selected visually, it doesn't actually trigger the event. Try using this with thewaitForText()
command for example; the program will timeout.What did work for me, was the code provided below (thanks @ArtjomB). How to fill a select element which is not embedded in a form with CasperJS?
Although, I think CasperJS should provide native support to select options from a drop-down when they aren't apart of a form (http://docs.casperjs.org/en/latest/modules/index.html). Selenium offers the
select
andaddSelection
commands (https://seleniumhq.github.io/selenium/docs/api/javascript/index.html). I've also filed a pending issue ticket on the CasperJS GitHub page to implement this natively (https://github.com/n1k0/casperjs/issues/1390).I don't know if you found a solution to your problem, but here is how I would solve it:
vanilla javascript solution (triggering onchange method):
Got same issue here. My solution is:
tejesh95's solution worked for me with a few minor changes, #1 I couldn't get findElementById to work so switched it to 'document.querySelector('#selectorId)' . I also had to change 'onchange' to 'onclick'. Here's the code within the casperjs test harness:
})
Follow on note: Your mileage may vary on the above, I found inconsistent results elsewhere, the text value would update, but the underlying control wouldn't fire. Frustrating!