我試圖抓取的尺寸爲這款產品:如何單擊「選擇選項」,然後評估加載的內容與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