2

How can I get Xpath Count in Selenium WebDriver?

This is my HTML page

<tbody xmlns="http://www.w3.org/1999/xhtml">
<tr class="odd">
    <td>1</td>
    <td>Cash</td>
    <td>
        <input type="checkbox" id="p1" checked="checked" value="1" name="productAccess"/>
        <input type="hidden" value="1" name="__checkbox_productAccess"/>
    </td>
</tr>
<tr class="even">
    <td>2</td>
    <td>Saving</td>
    <td>
        <input type="checkbox" id="p2" checked="checked" value="2" name="productAccess"/>
        <input type="hidden" value="2" name="__checkbox_productAccess"/>
    </td>
</tr>

I used

int product_count = driver.findElements(By.xpath("//id('product')/x:tbody/x:tr[1]")).size();

But always I am getting wrong xpath, anybody can help me?

2
  • 1
    What are you trying to count, exactly? What is the expected output in your example? May 30, 2013 at 12:11
  • I want to check the check boxes,if i get check box counts i can use loop..i think that is easy...same way i used in junit,but here my Xpather is not working properly :(
    – Namitha
    May 30, 2013 at 12:14

3 Answers 3

4

The id() function must but the first thing in the expression for it to work properly.

Also, I'm not exactly sure what the spec says about the namespace specifiers - when I tried it in my Firefox 24 (with Firebug + Firepath), it didn't accept those and it worked well without them.

id('product')/tbody/tr[1]

That said, your XPath is still wrong and doesn't match both the checkboxes. It matches the first tr element. In order to match the checkboxes, try

id('product')/tbody//input[@type='checkbox']

or find by CSS selector (which is shorter and faster):

#product > tbody input[type='checkbox']

All that said, if you simply want to loop over the checkboxes, you don't have to count them in the first place: (Java example)

List<WebElement> checkboxes = driver.findElements(By.cssSelector("#product > tbody input[type='checkbox']"));
for (WebElement checkbox : checkboxes) {
    // do your stuff with them
}

EDIT to adress OP's comments below

"...I want text from the table..."

No problem.

id('product')//td[following::td[1]/input[@type='checkbox']]

This is an XPath expression that matches only the <td> elements with the text you want to get. Literally, it matches "all <td> nodes that are descendants of #product and that are immediatelly followed by a <td> with a checkbox".

Now, you can simply loop over the found elements and invoke getText() on them.


If you'll continue using WebDriver, you'll soon find out that you definitely need to learn CSS selectors and/or XPath expressions (which are stronger).

There is a ton of tutorials out there, I'm sure you'll be able to find some. So I'll just point you to the specs which are, I think, the most useful resource:

8
  • Thanks for your reply,Here i want to check 'saving','cash'.how can i possible that,bse how can i get this values from html
    – Namitha
    May 31, 2013 at 6:24
  • @Namitha I'm sorry, I'm not sure what you want to get from what. Do you want to get the text ("Cash" and "Saving") from the table, or do you want to find the checkboxes next to "Cash" and "Saving" (both of which you know beforehand)? Or something a little bit diferent? Could you please clarify what is your input and what should be your output? May 31, 2013 at 9:10
  • Thanks your reply...:),I want text from the table,can you help me?
    – Namitha
    May 31, 2013 at 9:52
  • @Namitha You're welcome! I edited my answer with the solution. May 31, 2013 at 10:49
  • Thanks ...I am using Firefox 10.0.1,but my xpather is 0.4.4,so Iam not able to find the Xpath
    – Namitha
    May 31, 2013 at 11:01
2

To Check For the Size List checkboxes = driver.findElements(By.cssSelector("#product > tbody input[type='checkbox']"));

int no_of_checkboxes = checkboxes.size();

2
 Take the TR Count

 int icount = driver.FindElement(By.XPath("\tbody\tr")).Count; 

 int icheckbox_counter = 0;

 for(int i ; i<icount ;i++)
  {

  string strType = driver.FindElement(By.XPath("\tbody\tr["+icount+"]\td[3]")).GetAttribut("type")

   if(strType.Trim() == "checkbox")
    {
     icheckbox_counter++;
    }
  }

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.