Webdriver Check if Checkbox is Set and Set it if Not

  • I am learning Webdriver with JUnit by going through Alan Richardson's Selenium Simplified book and taking translating the exercises/tests from Selenium RC to Webdriver. So far, this has proven to be an excellent learning experience. But recently I have hit an issue, and cannot solve it despite extensive searching.

    In Selenium RC there is a selenium.check command which will check a box if it is empty, or leave it if it is already checked. For example:

    selenium.check("//input[@name=’checkboxes[]’ and @value=’cb3’]");
    

    If you go to the target website (http://compendiumdev.co.uk/selenium/basic_html_form.html) you will see there are three check boxes, one of which is checked. I have written some pseudo-code but just can't get into making it work. Here is the pseudo-code:

    isChecked = driver.findElement(By.xpath("//input[@type='checkbox']"));
    
    if (isChecked = false) {
        check the box;
    } else {
        do nothing;
    }
    

    I know the boolean isSelected() should feature, but I don't know where to go beyond that. All my research has returned partial solutions, but nothing that gets close enough for me to take a leap of faith at the solution.

    I am a little confused in what the issue exactly is. Are you not able to toggle the checkbox or are you not able to verify whether the checkbox is checked? Also if you haven't yet looked at, this doc might assist you - http://www.jarvana.com/jarvana/view/org/seleniumhq/selenium/selenium-common/2.0a2/selenium-common-2.0a2-javadoc.jar!/org/openqa/selenium/WebElement.html

    @SheyMouse, it may help others helping you if you write down the code you tried, the expected result, and actual result, as you were reporting a bug ;-)

    @SuchitParikh Thanks for the link. I am learning to read documentation, but application of the knowledge is still a bit rusty.

    @dzieciou Thank you for your advice. I had been attempting to do what you mentioned, but wasn't making progress. I am getting better at debugging my code, but this was one time I couldn't it fixed.

  • eviltester

    eviltester Correct answer

    8 years ago

    In the Selenium Simplified course the secret to the selector is really the 'value' not the 'type' as the 'value' identifies the WebElement uniquely on that page, coupled with a type 'just in case'

    WebElement checkBox1;
    WebElement checkBox3;
    
    checkBox1 = driver.findElement(By.cssSelector("input[value='cb1']"));
    checkBox3 = driver.findElement(By.cssSelector("input[value='cb3']"));
    
    if(!checkBox1.isSelected()){
        checkBox1.click();
    }
    
    //checkBox3 is selected by default
    if(checkBox3.isSelected()){
        checkBox3.click();
    }
    

    Or using findElements code.

    You could instead do:

    List<WebElement> selectElements= 
    driver.findElements(By.cssSelector("input[name='checkboxes[]']"));
    
    selectElements.get(0).click();
    
    if( selectElements.get(2).isSelected()){
        selectElements.get(2).click();
    }
    

    Or iterate over them:

    for(WebElement checkbox : selectElements){
        // uncheck 'em all
        if(checkbox.isSelected()){
          checkbox.click();
        }
    }
    

    Hope that helps.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM