我正在尝试使用casperJS
在下拉列表中选择和选项。我的网页
<select data-role="none" id="selectStatus">
<option value="SINGLE">Single</option>
<option value="MARRIED_FILING_JOINTLY">Married Filing Jointly</option>
<option value="MARRIED_FILING_SINGLY">Married Filing Separately</option>
<option value="HEAD_OF_HOUSEHOLD">Head of Household</option></select>
我的 JS 是
var status = 2; // Single , Married Filing Jointly , Married Filing Separately , Head of Household
//trying different ideas here...
if(status == 1){
status = 'SINGLE';
}else if(status == 2){
status = 'HEAD_OF_HOUSEHOLD';
}else if(taxFilingStatus == 3){
status = 'MARRIED_FILING_JOINTLY';
}else if(taxFilingStatus == 4){
status = 'MARRIED_FILING_SINGLY';
}
this.evaluate(function () {
$('#selectStatus').val(status).change();
});
this.echo(status);
this.capture(CapturePath('StatusSelected.png'));
当我作为变量传递时,我没有选择状态,但是当我像这样传递变量时
$('#selectStatus').val('SINGLE').change();
任何提示这里有什么问题?传递字符串有什么限制吗?
您不会在page.evaluate
内部传递status
变量,因此它在浏览器上下文中未定义,因此不起作用。
通过它的正确方法:
this.evaluate(function (status) {
$('#selectStatus').val(status).change();
}, status);