更新时间:2022-05-23 22:41:25
CasperJS' fill
函数只能使用该值。在您的情况下,这不起作用,因为您尝试设置显示的值而不是指定的选项值。虽然,这可以很容易地扩展:
CasperJS' fill
functions only work by using the value. In your case this doesn't work because you're trying to set the shown value not the assigned option value. Though, this can be easily extended:
casper.selectOptionByText = function(selector, textToMatch){
this.evaluate(function(selector, textToMatch){
var select = document.querySelector(selector),
found = false;
Array.prototype.forEach.call(select.children, function(opt, i){
if (!found && opt.innerHTML.indexOf(textToMatch) !== -1) {
select.selectedIndex = i;
found = true;
}
});
}, selector, textToMatch);
};
casper.start(url, function() {
this.selectOptionByText('form[name="formular"] select[name="category_group"]', "Motoren");
}).run();
参见此代码,用于SO联系页面上的完整工作示例。
See this code for a fully working example on the SO contact page.