2

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");
}
4
  • There are no input elements in the code. Are you trying to list <span> elements with the jqx-checkbox class?
    – FDM
    Commented Jun 17, 2016 at 14:40
  • Ya there are no input elements just div and span they mentioned
    – Mallika
    Commented Jun 17, 2016 at 14:44
  • Dynamically adding the grid check boxes as the data comes from database
    – Mallika
    Commented Jun 17, 2016 at 14:45
  • Then can you post the HTML with a dynamically added check box?
    – FDM
    Commented Jun 17, 2016 at 17:10

2 Answers 2

1

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']"))
8
  • cssSelector is not mentioned only class is given in div and that too runtime
    – Mallika
    Commented Jun 16, 2016 at 13:14
  • cssSelector is a way to find elements. Could you paste example of html code in comment? Example of selected and unselected checkbox
    – kotoj
    Commented Jun 16, 2016 at 13:15
  • Please provide both selected and unselected html code. Please paste it in comment, not edit my answer
    – kotoj
    Commented Jun 17, 2016 at 9:36
  • <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_
    – Mallika
    Commented Jun 17, 2016 at 14:27
  • In comments its accepting 250 char only.The div has more than 400 char
    – Mallika
    Commented Jun 17, 2016 at 14:28
0

The isSelected works with attributes of HTML. The xpath or other else way is much better to use. Here is Selenium Doc.

Here is html:

<input id="foo" type="checkbox">

Here is code:

var foo = element(by.id('foo'));
expect(foo.isSelected()).toBe(false);
foo.click();
expect(foo.isSelected()).toBe(true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.