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>
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.
The id of an element is unique within a page, so the id selector is used to select one unique element!
The CSS selector list ( , ) selects all the matching nodes. To reduce the size of style sheets, one can group selectors in comma-separated lists.
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.
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);
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With