Xpath for counting number of rows in a table (WebDriver)

16,659 views
Skip to first unread message

MySelenium

unread,
Nov 8, 2012, 6:21:30 AM11/8/12
to seleniu...@googlegroups.com
Hello all,

Do you know how to form an Xpath that counts the number of rows in a table, and read the value into WebDriver Java ?

I don't like the following way:

WebDriver.findElements(By.xpath("//table[@id='table_id']/tr")).size()

and I DO like the following way

WebDriver.someMethod(By.xpath("count(//table[@id='table_id']/tr"))

Many thanks to you
My,

Krishnan Mahadevan

unread,
Nov 8, 2012, 6:27:56 AM11/8/12
to seleniu...@googlegroups.com
public static countNumberOfRowsInTable(String xPath){
     WebDriver.findElements(By.xpath(xPath)).size();
}

SomeClass.countNumberOfRowsInTable("//table[@id='table_id']/tr");


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
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 https://groups.google.com/groups/opt_out.
 
 

MySelenium

unread,
Nov 8, 2012, 4:06:03 PM11/8/12
to seleniu...@googlegroups.com
Hi Krishnan,

Please note that what I like MUST HAVE the XPATH count() function in its xpath.

Yours,

Mark Collin

unread,
Nov 8, 2012, 4:43:56 PM11/8/12
to seleniu...@googlegroups.com

Count is not valid as a locator, you are trying to do something that is not possible.

 

A By object is a locator used to find an element in the DOM, returning a number as an element location is nonsense and will not work

Mike Riley

unread,
Nov 8, 2012, 7:17:06 PM11/8/12
to seleniu...@googlegroups.com
findElements(By.whatever("something")).size() gives you the count.  That is what his code showed you.  In your case whatever would be xpath and "something" would be the XPath string you wish to use.

Mike

MySelenium

unread,
Nov 9, 2012, 4:17:43 AM11/9/12
to seleniu...@googlegroups.com
Using .size() on a Java Collection is what I don't want to do.

I said in my first email that I would like to use the Xpath count function

http://www.w3.org/TR/xpath/#function-count

So this is what I am looking for

WebDriver.someMethod("count(//table[@id='table_id']/tr")

Thanks
My,
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/BWARSWFrr_IJ.

MySelenium

unread,
Nov 9, 2012, 4:21:13 AM11/9/12
to seleniu...@googlegroups.com
Yeah, so using a By object on a number is not possible, but how about not using a By object,

Is this possible ?


WebDriver.someMethod("count(//table[@id='table_id']/tr")

Thanks
My,

Krishnan Mahadevan

unread,
Nov 9, 2012, 4:23:40 AM11/9/12
to seleniu...@googlegroups.com
AFAIK there arnt any methods in the WebDriver API that lets you do what are expecting, out of the box!
By Object is the ONLY way of querying the DOM that WebDriver provides you with. 
My Scribblings @ http://wakened-cognition.blogspot.com/

MySelenium

unread,
Nov 9, 2012, 5:17:38 AM11/9/12
to seleniu...@googlegroups.com
So it means we cannot read out to Java the primitive value like number, string, boolean ?
Thanks

Krishnan Mahadevan

unread,
Nov 9, 2012, 5:20:09 AM11/9/12
to seleniu...@googlegroups.com
I admit now am lost as to what are you actually looking here for an answer ! 

I was stating that without using a By object you CANNOT locate an element using WebDriver APIs. 
--
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 https://groups.google.com/groups/opt_out.
 
 

MySelenium

unread,
Nov 9, 2012, 5:42:05 AM11/9/12
to seleniu...@googlegroups.com
Hi,


On 11/09/2012 11:20 AM, Krishnan Mahadevan wrote:

I was stating that without using a By object you CANNOT locate an element using WebDriver APIs. 

Yeah, so if I understand correctly:

- all WebDriver APIs do is to give us the DOM node/element, and to do that it refuses to work with certain kind of xpath, for example the "count(//tr)"
- Using By is the only way what our WebDriver API communicates with the DOM engine

Thanks

Krishnan Mahadevan

unread,
Nov 9, 2012, 5:55:05 AM11/9/12
to seleniu...@googlegroups.com
Count() fn is NOT a method to identify an element. Why would you expect WebDriver's By Object to support xpath functions which are NOT related to helping you search for elements on page? 


On Friday, November 9, 2012, MySelenium wrote:
--
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 https://groups.google.com/groups/opt_out.
 
 

Jim Evans

unread,
Nov 9, 2012, 6:10:24 AM11/9/12
to seleniu...@googlegroups.com
I fail to see why getting the size of the collection returned by findElements() won't suit your purpose. Both that and the raw use of the XPath count() function should give you a scalar value back to your Java code to be used for whatever you need it for. Perhaps if you justified that statement, it might be easier to understand.

Having said that, you could try using JavascriptExecutor.executeScript() to call document.evaluate() on your document. That code would look something like this:

// WARNING: Untested code written from
// memory without benefit of an IDE! May
// not run or compile without modification.
int count = (int)((JavascriptExecutor)driver).executeScript("return document.evaluate('count(//tr)');");

HOWEVER, and this is a big however, this code will only work for browsers that have a native XPath-over-HTML engine. At the moment, that specifically excludes IE, and may also exclude others.

--Jim

Dragisa Krtolica

unread,
Nov 9, 2012, 1:35:47 PM11/9/12
to seleniu...@googlegroups.com
WebDriver.someMethod("count(//table[@id='table_id']/tr")   - you get  count

or if you do
 WebDriver driver = new FirefoxDriver();
WebDriver.someMethod(driver.findElements(.By.xpath('//table[@id='table_id']/tr")).size()) - also gives you count

Why do you insist on using  xpath count and not using .size()?  What are the advantages? 

Asha Nadiminti

unread,
Oct 21, 2013, 9:07:22 AM10/21/13
to seleniu...@googlegroups.com
Hi All,

It is not supporting size() rather if we give getsize() it will give the dimension.
Reply all
Reply to author
Forward
0 new messages