Check Box check and Uncheck using selenium web driver



  • I am not able to list out the check boxes which are checked and unchecked.the checkboxes are placed in the div tags. And only class name is generated dynamically when the check box is clicked.

    Unchecked checkboxes:

    <div class="jqx-checkbox-default jqx-checkbox-default-p_p_id=TeamManagement_WAR_TeamManagementportlet&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_TeamManagement_WAR_TeamManagementportlet_artifactTypeLabel=0&_TeamManagement_WAR_TeamManagementportlet_jspPage=%2Fhtml%2Fteam%2Fassign_user.jsp&_TeamManagement_WAR_TeamManagementportlet_token=ORGANIZATION&_TeamManagement_WAR_TeamManagementportlet_screenMode=maximized&_TeamManagement_WAR_TeamManagementportlet_artifactId=0&_TeamManage...l-p_p_id=TeamManagement_WAR_TeamManagementportlet&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_TeamManagement_WAR_TeamManagementportlet_artifactTypeLabel=0&_TeamManagement_WAR_TeamManagementportlet_jspPage=%2Fhtml%2Fteam%2Fassign_user.jsp&_TeamManagement_WAR_TeamManagementportlet_token=ORGANIZATION&_TeamManagement_WAR_TeamManagementportlet_screenMode=maximized&_TeamManagement_WAR_TeamManagementportlet_artifactId=0&_TeamManagement_WAR_TeamManagementportlet_teamId=0">
    <div style="width: 13px; height: 13px;">
    <span class="" style="width: 13px; height: 13px;"></span>
    </div>
    

    Checked checkboxes:

    <div class="jqx-checkbox-default jqx-checkbox-default-p_p_id=TeamManagement_WAR_TeamManagementportlet&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_TeamManagement_WAR_TeamManagementportlet_artifactTypeLabel=0&_TeamManagement_WAR_TeamManagementportlet_jspPage=%2Fhtml%2Fteam%2Fassign_user.jsp&_TeamManagement_WAR_TeamManagementportlet_token=ORGANIZATION&_TeamManagement_WAR_TeamManagementportlet_screenMode=maximized&_TeamManagement_WAR_TeamManagementportlet_artifactId=0&_TeamManagement_WAR_TeamManagementportlet_teamId=0 jqx-fill-state-normal jqx-fill-state-normal-p_p_id=TeamManagement_WAR_TeamManagementportlet&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_TeamManagement_WAR_TeamManagementportlet_artifactTypeLabel=0&_TeamManageme`nt_WAR_TeamManagementportlet_jspPage=%2Fhtml%2Fteam%2Fassign_user.jsp&_TeamManagement_WAR_TeamManagementportlet_token=ORGANIZATION&_TeamManagement_WAR_TeamManagementportlet_screenMode=maximized&_TeamManagement_WAR_TeamManagementportlet_artifactId=0&_TeamManagement_WAR_TeamManagementportlet_teamId=0 jqx-rc-all jqx-rc-all-p_p_id=TeamManagement_WAR_TeamManagementportlet&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_TeamManagement_WAR_TeamManagementportlet_artifactTypeLabel=0&_TeamManagement_WAR_TeamManagementportlet_jspPage=%2Fhtml%2Fteam%2Fassign_user.jsp&_TeamManagement_WAR_TeamManagementportlet_token=ORGANIZATION&_TeamManagement_WAR_TeamManagementportlet_screenMode=maximized&_TeamManagement_WAR_TeamManagementportlet_artifactId=0&_TeamManagement_WAR_TeamManagementportlet_teamId=0">
    <div style="width: 13px; height: 13px;">
    <span class="jqx-checkbox-check-checked jqx-checkbox-check-checked-p_p_id=TeamManagement_WAR_TeamManagementportlet&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_TeamManagement_WAR_TeamManagementportlet_artifactTypeLabel=0&_TeamManagement_WAR_TeamManagementportlet_jspPage=%2Fhtml%2Fteam%2Fassign_user.jsp&_TeamManagement_WAR_TeamManagementportlet_token=ORGANIZATION&_TeamManagement_WAR_TeamManagementportlet_screenMode=maximized&_TeamManagement_WAR_TeamManagementportlet_artifactId=0&_TeamManagement_WAR_TeamManagementportlet_teamId=0" style="width: 13px; height: 13px;"></span>
    </div>
    

    The code:

    //Checked element is present in this div
    WebElement weChecked=driver.findElement(By.xpath("//div[3]/div[3]/div/div[5]/div/div/div[1]/div")); 
    //Unchecked element is present in this div
    WebElement weUnchecked=driver.findElement(By.xpath("//div[3]/div[3]/div/div/div[1]/div"));
    
    //This the following code where I am checking
    WebElement weUnchecked=driver.findElement(By.xpath("//div[3]/div[3]/div/div/div[1]/div"));
    if(weUnchecked.isSelected())
    {
        System.out.println("Selected");
    }
    else
    {
        System.out.println("Not Selected");
    }
    //WebElement weChecked=driver.findElement(By.xpath("//div[3]/div[3]/div/div[5]/div[1]/div"));
    WebElement weChecked=driver.findElement(By.xpath("//div[3]/div[3]/div/div[5]/div/div/div[1]/div"));
    if(weChecked.isSelected())
    {
        System.out.println("Selected");
    }
    else
    {
        System.out.println("Not Selected");
    }
    


  • The simpliest way is using CSS selector instead of xpath in this case.

    driver.findElements(By.cssSelector("input:checked[type='checkbox']"))
    

    should return the list with all selected checkboxes.

    And this one for not selected:

    driver.findElements(By.cssSelector("input:not(:checked)[type='checkbox']"))
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2