Question on using .size() for counting webelement

46 views
Skip to first unread message

Cheuk Chan

unread,
Jul 29, 2015, 9:17:27 PM7/29/15
to Selenium Users
I have this code running in firefox browser,

           System.out.println(driver.findElements(By.xpath("//*[@class='product-box']")).size());

It was working good and give me the correct count of that webelement but once I run this code via chrome. It is always giving 0. 

Is there any problem of running this code?


Thanks!

Krishnan Mahadevan

unread,
Jul 30, 2015, 4:41:08 AM7/30/15
to Selenium Users
You might want to start off by checking if that XPath is valid in Firefox.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/bb2bc8d4-9935-46a7-9c4d-720956c0a245%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sudhansu Sekhar panda

unread,
Jul 30, 2015, 8:21:02 AM7/30/15
to Selenium Users, cheuk...@inturn.co
Hi Chan,

Might be the point is not loaded completely and before it loads you are finding that element.

Also please check the xpath in Chrome whether that is working or not..

Thanks,

Sudhansu

Jon Thor Austen

unread,
Jul 30, 2015, 10:51:22 AM7/30/15
to Selenium Users, cheuk...@inturn.co
I think that since each browser has a different XPath engine, that the solution is likely just changing this:
By.xpath("//*[@class='product-box']")

to this:
By.xpath(".//*[@class='product-box']")

Also, I avoid using '*' char in locators because when you get an error , its not as easy to tell what it is.  The more specific your locator is, the easier it is to look at it and say "i remember what that was" .
-Jon

Kaleem Uddin Mohammed Abdul

unread,
Aug 3, 2015, 1:59:40 AM8/3/15
to Selenium Users
I agree with Sudhansu here. The problem may be the control is not loaded completely.
Reply all
Reply to author
Forward
0 new messages