Count of the check boxes...

762 views
Skip to first unread message

NagaRaju dasam

unread,
Dec 8, 2010, 4:38:37 AM12/8/10
to Selenium Users

Hi All,

I am trying to find the number of check boxes and the check boxes checked..in a site....can u guys help me which command i need to use?

http://browsershots.org/  in this site i want to know number of check boxes and the number of check boxes which are chekced....
--
Thanks,
Naga


Elias Nogueira

unread,
Dec 8, 2010, 8:01:51 AM12/8/10
to seleniu...@googlegroups.com
If you use Sel_IDE the command is assertXpathCount, but it's an assertion and you need put the expected value.
In Java the command is getXpathCount()

Regards!
--
Elias Nogueira




--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

NagaRaju dasam

unread,
Dec 8, 2010, 8:39:28 AM12/8/10
to seleniu...@googlegroups.com
Thanks for the reply...I am using RC with Java

so getXpathCount()...what is the input i need to give in ()....which name i need to give

Thanks,
Naga

Elias Nogueira

unread,
Dec 8, 2010, 8:48:17 AM12/8/10
to seleniu...@googlegroups.com
If you need get all the ckeckbox try it: //input[@type['checkbox']]

Regards!
--
Elias Nogueira

Timmay

unread,
Dec 8, 2010, 10:38:48 AM12/8/10
to Selenium Users
if you want to know how many checkboxes there are in total, use xpath:
final int total = this.getXpathCount("id('startform')//
input[@type='checkbox']").intValue();

if you want to know how many checkboxes are selected, also use xpath:
final int selected = this.getXpathCount("id('startform')//
input[@type='checkbox' and @checked]").intValue();

I don't think there is a default way to get the list of all checkboxes
that are selected, but you could try using javascript for that:
final String script =
"var checked = new Array();"
+ "var cnt = 0;"
+ "var form = document.getElementById('startform');"
+ "for(var i = 0; i < form.elements.length; i++) {"
+ " if(form.elements[i].getAttribute('type') ==
'checkbox'"
+ " && form.elements[i].checked) {"
+ " checked[cnt] = form.elements[i].id;"
+ " cnt++;"
+ " }"
+ "}"
+ "checked.toString();";
final String[] elements = this.getEval(script).split(",");

BillR

unread,
Dec 8, 2010, 12:57:00 PM12/8/10
to Selenium Users
> checked[cnt] = form.elements[i].id;

Is there a way to get the XPath of the element instead of its id?
> >http://testerinyou.blogspot.com/- Hide quoted text -
>
> - Show quoted text -

BillR

unread,
Dec 8, 2010, 4:24:14 PM12/8/10
to Selenium Users
I don't see anything on javascript xpath generation in the O'Reilly
Javascript guide. A useful alternative for me would be if it were
possible to make the javascript code click on the element.
> > >http://testerinyou.blogspot.com/-Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -

NagaRaju dasam

unread,
Dec 9, 2010, 12:51:45 AM12/9/10
to seleniu...@googlegroups.com
It worked below is my code...

Number c  =selenium.getXpathCount("//input[@type['checkbox']]");
System.out.println("Count of check boxes " +c);
Number d = selenium.getXpathCount("//input[@type='checkbox' and @checked]");
System.out.println("Count of Checked check boxes " +d);
}


To find unchecked check boxes what i need to use?

Number d = selenium.getXpathCount("//input[@type='checkbox' and @notchecked]"); ---but it didnt work...any other way to find unchecked checkboxes??

Thanks,
Naga

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

NagaRaju dasam

unread,
Dec 9, 2010, 1:25:12 AM12/9/10
to seleniu...@googlegroups.com
Hey i just modified cod with soem changes..it worked...

Number c  =selenium.getXpathCount("//input[@type['checkbox']]");
System.out.println("Count of check boxes " +c);
Number d = selenium.getXpathCount("//input[@type='checkbox' and @checked]");
System.out.println("Count of Checked check boxes " +d);
Number e = selenium.getXpathCount("//input[@type='checkbox' and not(@checked)]");
System.out.println("Count of Checked check boxes " +e);
}

Thanks all for your suggestions....

NagaRaju dasam

unread,
Dec 9, 2010, 5:11:06 AM12/9/10
to seleniu...@googlegroups.com
Another problemm

If i uncheck some check boxes and try the the script it is not working....bcos if you check and uncheck this wouldn't change any thing in their html....

can any one help me...


Thanks,
Naga

sai krishna

unread,
Dec 9, 2010, 5:47:37 AM12/9/10
to Selenium Users
How do we print the values of that checkbox .. i mean the text
value ..

On Dec 9, 3:11 pm, NagaRaju dasam <nagaselen...@gmail.com> wrote:
> Another problemm
>
> If i uncheck some check boxes and try the the script it is not
> working....bcos if you check and uncheck this wouldn't change any thing in
> their html....
>
> can any one help me...
>
> site URl ishttp://browsershots.org/
>
> Thanks,
> Naga
>
> On Thu, Dec 9, 2010 at 11:55 AM, NagaRaju dasam <nagaselen...@gmail.com>wrote:
>
>
>
> > Hey i just modified cod with soem changes..it worked...
>
> > Number c  =selenium.getXpathCount("//input[@type['checkbox']]");
> >  System.out.println("Count of check boxes " +c);
> > Number d = selenium.getXpathCount("//input[@type='checkbox' and
> > @checked]");
> >  System.out.println("Count of Checked check boxes " +d);
> > Number e = selenium.getXpathCount("//input[@type='checkbox' and
> > not(@checked)]");
> >  System.out.println("Count of Checked check boxes " +e);
> > }
>
> > Thanks all for your suggestions....
>
> > On Thu, Dec 9, 2010 at 11:21 AM, NagaRaju dasam <nagaselen...@gmail.com>wrote:
>
> >> It worked below is my code...
>
> >> Number c  =selenium.getXpathCount("//input[@type['checkbox']]");
> >>  System.out.println("Count of check boxes " +c);
> >>  Number d = selenium.getXpathCount("//input[@type='checkbox' and
> >> @checked]");
> >> System.out.println("Count of Checked check boxes " +d);
> >>  }
>
> >> To find unchecked check boxes what i need to use?
>
> >> Number d = selenium.getXpathCount("//input[@type='checkbox' and
> >> @notchecked]"); ---but it didnt work...any other way to find unchecked
> >> checkboxes??
>
> >> Thanks,
> >> Naga
>
> >>> > > >http://browsershots.org/in this site i want to know number of
> >>> check boxes
> >>> > > > and the number of check boxes which are chekced....
> >>> > > > --
> >>> > > > Thanks,
> >>> > > > Naga
>
> >>> > > >http://testerinyou.blogspot.com/-Hidequoted text -
>
> >>> > > - Show quoted text -- Hide quoted text -
>
> >>> > - Show quoted text -
>
> >>> --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "Selenium Users" group.
> >>> To post to this group, send email to seleniu...@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>> selenium-user...@googlegroups.com<selenium-users%2Bunsu...@googlegroups.com>
> >>> .

Timmay

unread,
Dec 9, 2010, 6:22:17 AM12/9/10
to Selenium Users
Try using the JavaScript I supplied before.
As you said, the HTML won't change, so the xpath won't work when you
(un)check a checkbox.
But the DOM will change, and the JavaScript will be able to see this.

Just run the script and check the length of the array.


On Dec 9, 11:11 am, NagaRaju dasam <nagaselen...@gmail.com> wrote:
> Another problemm
>
> If i uncheck some check boxes and try the the script it is not
> working....bcos if you check and uncheck this wouldn't change any thing in
> their html....
>
> can any one help me...
>
> site URl ishttp://browsershots.org/
>
> Thanks,
> Naga
>
> On Thu, Dec 9, 2010 at 11:55 AM, NagaRaju dasam <nagaselen...@gmail.com>wrote:
>
>
>
> > Hey i just modified cod with soem changes..it worked...
>
> > Number c  =selenium.getXpathCount("//input[@type['checkbox']]");
> >  System.out.println("Count of check boxes " +c);
> > Number d = selenium.getXpathCount("//input[@type='checkbox' and
> > @checked]");
> >  System.out.println("Count of Checked check boxes " +d);
> > Number e = selenium.getXpathCount("//input[@type='checkbox' and
> > not(@checked)]");
> >  System.out.println("Count of Checked check boxes " +e);
> > }
>
> > Thanks all for your suggestions....
>
> > On Thu, Dec 9, 2010 at 11:21 AM, NagaRaju dasam <nagaselen...@gmail.com>wrote:
>
> >> It worked below is my code...
>
> >> Number c  =selenium.getXpathCount("//input[@type['checkbox']]");
> >>  System.out.println("Count of check boxes " +c);
> >>  Number d = selenium.getXpathCount("//input[@type='checkbox' and
> >> @checked]");
> >> System.out.println("Count of Checked check boxes " +d);
> >>  }
>
> >> To find unchecked check boxes what i need to use?
>
> >> Number d = selenium.getXpathCount("//input[@type='checkbox' and
> >> @notchecked]"); ---but it didnt work...any other way to find unchecked
> >> checkboxes??
>
> >> Thanks,
> >> Naga
>
> >>> > > >http://browsershots.org/in this site i want to know number of
> >>> check boxes
> >>> > > > and the number of check boxes which are chekced....
> >>> > > > --
> >>> > > > Thanks,
> >>> > > > Naga
>
> >>> > > >http://testerinyou.blogspot.com/-Hidequoted text -
>
> >>> > > - Show quoted text -- Hide quoted text -
>
> >>> > - Show quoted text -
>
> >>> --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "Selenium Users" group.
> >>> To post to this group, send email to seleniu...@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>> selenium-user...@googlegroups.com<selenium-users%2Bunsu...@googlegroups.com>
> >>> .

Meerasaaheb Mohmmad

unread,
Dec 20, 2010, 7:32:27 AM12/20/10
to Selenium Users, farheen khan, mohmmad Meerasaab
HI dude ,

Use the following code to get your requirement done....

import com.thoughtworks.selenium.DefaultSelenium;
public class CheckBoxesStatusTest {
public static void main(String[] args) {
DefaultSelenium selenium = new DefaultSelenium("localhost", 8888,
"iexplore","http://browsershots.org/");
selenium.start();
selenium.open("http://browsershots.org/");
selenium.waitForPageToLoad("30000");
String checkBoxIds =selenium.getEval("function getCheckBoxsStatus()
{" //This is the function which will give the id's of of all check
boxes which are checked and unchecked
+"var cbgChkRefid='checked:';" // Some start variable for
check boxes check status
+"var chkCounter=0;"//counter for checked check boxes
+"var cbgNonChkRefid='unchecked:';" // Some start variable for
check boxes uncheck status
+"var unchkCounter=0;"//counter for unchecked check boxes
+"var docref = window.document;" // This variable holds the
document reference of a current selenium window
+"var
ipRef=docref.getElementById('startform').getElementsByTagName('input');" //
This variable holds the all input fields available in a form with id
'startform'
+"for(var k=0;k<ipRef.length;k++){"// This is a for loop for
iteration of the elements of the form
+"if(ipRef[k].type=='checkbox'){"// This if checks the input
element inside the form is check box or not
+"if(ipRef[k].checked){"// This if checks the input element
inside the form is check box and is checked
+"chkCounter=chkCounter+1;"
+"cbgChkRefid=cbgChkRefid+' '+ipRef[k].id;"// This variable
holds the string concatinating of the ids of the checked fields
+"}"// close of checked if
+"else{"// This if checks the input element inside the form is
check box and is unchecked
+"unchkCounter=unchkCounter+1;"
+"cbgNonChkRefid=cbgNonChkRefid+' '+ipRef[k].id;"// This
variable holds the string concatinating of the ids of the unchecked
fields
+"}"// close of else
+"}"// close of check box if if the input is of type check box
+"}"// End of for loop
+"return (cbgChkRefid+' cheked Checkboxes
Number'+chkCounter.toString()+'::'+cbgNonChkRefid+' uncheked
Checkboxes Number'+unchkCounter.toString()+'
totalNumberofcheckboxesonthePage:'+ipRef.length);"// Return statement
of the calling method getCheckBoxsStatus()
+"}"// close of function
+"getCheckBoxsStatus();");// invoking the method for getting
the checkboxes checked and unchecked as a String
checkBoxIds = checkBoxIds.replaceAll(":", "\n");
System.out.println("All checked and Unchecked IDS :\n
"+checkBoxIds.replaceAll(":", "\n"));
System.out.println("All checked and Unchecked IDS in line by line
format :\n "+checkBoxIds.replaceAll(" ", "\n"));
}
}

Let me know if you still need any help on this.....

BR,

Meerasaaheb Mohmmad.....

NagaRaju dasam

unread,
Dec 20, 2010, 8:05:29 AM12/20/10
to seleniu...@googlegroups.com
Thanks for the code...Meerasaaheb. Let u know if i need any info...

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

jerald

unread,
Dec 20, 2010, 8:46:35 AM12/20/10
to Selenium Users
Hi BillR,

to get the id use getAttribute("//input[@type=''checkbox]@id")

On Dec 20, 6:05 pm, NagaRaju dasam <nagaselen...@gmail.com> wrote:
> Thanks for the code...Meerasaaheb. Let u know if i need any info...
>
> On Mon, Dec 20, 2010 at 6:02 PM, Meerasaaheb Mohmmad <meerasaab...@gmail.com
> > >http://browsershots.org/in this site i want to know number of check
> > boxes
> > > and the number of check boxes which are chekced....
> > > --
> > > Thanks,
> > > Naga
>
> > >http://testerinyou.blogspot.com/
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Selenium Users" group.
> > To post to this group, send email to seleniu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > selenium-user...@googlegroups.com<selenium-users%2Bunsu...@googlegroups.com>
> > .

deter

unread,
Dec 21, 2010, 1:00:02 AM12/21/10
to seleniu...@googlegroups.com
Here is the complete code to count total number of check boxes, selected checkboxes, unchecked checkboxes:

package com.test;

import java.io.File;

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.BrowserConfigurationOptions;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class BasicTest {

    SeleniumServer server = null;
    Selenium selenium = null;

    @BeforeClass
    public void setUp() {
        RemoteControlConfiguration rcc = new RemoteControlConfiguration();
        rcc.setFirefoxProfileTemplate(new File(
                "C:\\Documents and Settings\\deter\\Application Data\\Mozilla\\Firefox\\Profiles\\5e5ry3s1.selenium"));
        try {
            server = new SeleniumServer(rcc);
            server.start();
        } catch (Exception e) {

            System.out
                    .println("Selenium server start up failed because of the below exception");
            System.out.println(e.getStackTrace());

        }
        BrowserConfigurationOptions options = new BrowserConfigurationOptions();
        options.setSingleWindow();
        selenium = new DefaultSelenium("localhost", 4444, "*chrome",
                "http://browsershots.org");
        selenium.start(options);
        selenium.open("/");

    }

    @Test
    public void testMethod() {

        int totalCheckBoxes = (Integer) selenium
                .getXpathCount("//div[@class='browser_list']//input[@type='checkbox']");
        int selectedCheckBoxes = (Integer) selenium
                .getXpathCount("//div[@class='browser_list']//input[contains(@type,'checkbox') and (@checked='checked')]");
        int unCheckedCheckBoxes = totalCheckBoxes - selectedCheckBoxes;

        System.out.println("Total number of check boxes " + totalCheckBoxes);
        System.out.println("Number of selected check boxes " + selectedCheckBoxes);
        System.out.println("Number of unchecked check boxes "+ unCheckedCheckBoxes);

    }

    @AfterClass
    public void tearDown() {
        selenium.stop();
        server.stop();

    }

}

Here is the link for formatted code: http://pastebin.com/880QRSZ4

NOTE: The libraries i used are : Selenium server 2.0a7,testng.


Reply all
Reply to author
Forward
0 new messages