我试图抓取的尺寸为这款产品:如何单击“选择选项”,然后评估加载的内容与casperjs
的问题:大小的颜色后装入该产品被选中。
在产品页面的源代码中,我可以看到下拉列表有一个onchange-method:它点击表格#postColor onchange。
选择下拉:
<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>
的#postColor形式,它被点击的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;">
这是到目前为止我的代码,它不工作:
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);
}
});
});
我怀疑这个问题是一个全新的页面被加载时点击一个颜色(大小不动态追加)。
你会如何解决这个问题?
这并不为我工作。它改变了选项下拉视觉(以及可与casper.capture可以看出),但它实际上并没有引发选定的值。 – tfmontague 2016-01-01 01:51:35
Perfec t谢谢 – CodeGuru 2016-11-16 13:09:28