208*_*083 11 javascript html-select phantomjs 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>
Run Code Online (Sandbox Code Playgroud)
#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;">
Run Code Online (Sandbox Code Playgroud)
到目前为止这是我的代码,它不起作用:
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);
}
});
});
Run Code Online (Sandbox Code Playgroud)
我怀疑问题是单击颜色时会加载一个全新的页面(不会动态附加大小).
你会如何解决这个问题?
wma*_*but 14
我就是这样做的
this.evaluate(function() {
$('#select_element_selector').val('value').change();
});
Run Code Online (Sandbox Code Playgroud)
这change()非常重要
我假设你在页面上有jQuery
小智 12
这里有同样的问题.我的解决方案是:
casper.then(function(){
this.evaluate(function() {
document.querySelector('select.select-category').selectedIndex = 2; //it is obvious
});
this.capture('screenshot.png');
});
Run Code Online (Sandbox Code Playgroud)