Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I count the number of elements that match my CSS selector?

I am trying to use SeleniumRC to test my GWT App and am trying to match elements using CSS selectors.

I want to count the number of enabled buttons in the following HTML.

A button is enabled if it is under a <td> with class="x-panel-btn-td " and disabled if it is under a <td> with class="x-panel-btn-td x-hide-offsets".

So basically, I want to retrieve the number of buttons under all <td>s with the class x-panel-btn-td.

<table cellspacing="0">     <tbody>     <tr>         <td id="ext-gen3504" class="x-panel-btn-td ">             <em unselectable="on">                 <button id="ext-gen3506" class="x-btn-text" type="button">OK</button>             </em>         </td>         <td id="ext-gen3512" class="x-panel-btn-td x-hide-offsets">             <em unselectable="on">                 <button id="ext-gen3506" class="x-btn-text" type="button">Yes</button>             </em>         </td>         <td id="ext-gen3520" class="x-panel-btn-td">             <em unselectable="on">                 <button id="ext-gen3506" class="x-btn-text" type="button">No</button>             </em>         </td>         <td id="ext-gen3528" class="x-panel-btn-td x-hide-offsets">             <em unselectable="on">                 <button id="ext-gen3506" class="x-btn-text" type="button">Cancel</button>             </em>         </td>     </tr>     </tbody> </table> 
like image 760
Nirmal Patel Avatar asked Oct 15 '09 15:10

Nirmal Patel


People also ask

How do I count elements in CSS?

In the CSS we are going to do three key things: Initialize the counter on the parent div using counter-reset. Increment the counter value by 1 on the child div p using counter-increment. Add the counter variables before the div p content using the :before pseudo selector.

How many elements does the ID selector refers to in CSS?

The id of an element is unique within a page, so the id selector is used to select one unique element!

What CSS selector matches all list items?

The CSS selector list ( , ) selects all the matching nodes. To reduce the size of style sheets, one can group selectors in comma-separated lists.

How do you use the nth element using the CSS selector?

The :nth-of-type(n) selector matches every element that is the nth child, of the same type (tag name), of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-child() selector to select the element that is the nth child, regardless of type, of its parent.


2 Answers

As far as I am aware you can't do this using CSS selectors, but there is a command in Selenium for counting by XPath. The following command will verify there are two disabled buttons:

verifyXpathCount | //td[contains(@class, 'x-hide-offsets')]//button | 2 

In Selenium RC (Java) this would look more like

assertEquals(selenium.getXpathCount("//td[contains(@class, 'x-hide-offsets')]//button"), 2); 
like image 166
Dave Hunt Avatar answered Sep 18 '22 05:09

Dave Hunt


This is now also implemented (without any extra Javascript magic needed) in Selenium Webdriver API Since google still links to this question as a top result, even though Selenium RC has been replaced by Webdriver, hopefully this saves someone time.

Example java code:

int locatorElementSize = driver.findElements(By.cssSelector("yourCSSLocator")).size(); 
like image 41
HRVHackers Avatar answered Sep 21 '22 05:09

HRVHackers