I want to click on a checkbox only if it is already checked.

The following code doesn't seem to work:

if(this.test.assert(this.evaluate(function () {return document.getElementById('myID').checked;}))) 
{           
    this.click('#myID');     
}

What's the correct syntax?

有帮助吗?

解决方案

The test.assert* functions are there for asserting a certain condition. Remove it because you want to branch the execution. Otherwise is your code correct.

test.assertExists("#myID");
// this function will not be executed if #myID wasn't there
var id = 'myID';
if(this.evaluate(function (pageCtxId) {return document.getElementById(pageCtxId).checked;}, id))
{           
    this.click('#'+id);     
}

其他提示

You might want to do it that way :

casper.waitFor(function checked() {
    return this.evaluate(function() {
        return document.getElementById('myID').checked === true;
    });
}, function then() {
    this.click('#myID');
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top