OpenQA.Selenium.IWebElement does not contain a definition for 'size'

4,993 views
Skip to first unread message

eric aldinger

unread,
Jul 30, 2012, 2:48:00 PM7/30/12
to seleniu...@googlegroups.com
I am getting an error stating there is no definition for size() on driver.FindElements. I see the docs state this is the prefered way to conditionally locate an item and take action. Am I not including some reference?

ERROR
Error    1    'System.Collections.ObjectModel.ReadOnlyCollection<OpenQA.Selenium.IWebElement>' does not contain a definition for 'size' and no extension method 'size' accepting a first argument of type 'System.Collections.ObjectModel.ReadOnlyCollection<OpenQA.Selenium.IWebElement>' could be found (are you missing a using directive or an assembly reference?)    c:\Source\GetScreenShots\Program.cs    439    78    GetScreenShots

CODE SNIPPET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Interactions.Internal;
using OpenQA.Selenium.Support.UI;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO;
using System.Collections;

//SKIP TO PROBLEM CODE

            if (driver.FindElements(By.Id("ctl00_mainContent_cancelButton")).size() > 1)
                //element found
            {           
                driver.FindElement(By.Id("ctl00_mainContent_cancelButton")).Click();
            }
                TakeScreenShot(driver, scenario);

Jim Evans

unread,
Jul 30, 2012, 2:57:33 PM7/30/12
to seleniu...@googlegroups.com
Language binding code is designed to be idiomatic of the language it's written in. That is, writing WebDriver code in a given language should feel natural to someone familiar with the language. When you return a collection in most .NET APIs, you're returning something that implements IList<T>, which ReadOnlyCollection<T> does. IWebDriver.FindElements() returns a ReadOnlyCollection<IWebElement>, so to check the number of elements in the collection, you use the .Count property (not the .size() method, which is a Java idiom).

Incidentally, there's a problem with your if() branch, in that you'll want a .Count > 0, if you're testing whether the element was found.

--Jim

eric aldinger

unread,
Jul 30, 2012, 3:57:52 PM7/30/12
to seleniu...@googlegroups.com
I am testing whether the element was found. The example I followed ussd > 1 and I had not tested it yet. Thanks for that.

On the idiom front, .size() was mentioned in several c# posts I read. Are you saying that. Size() was not implemented in the c# bindings?

Jim Evans

unread,
Jul 30, 2012, 4:05:12 PM7/30/12
to seleniu...@googlegroups.com
If you're seeing .size() (all lower-case, and presented as a method) in a C# example, then quite frankly, the example is just plain wrong. That doesn't look like any .NET API from Microsoft or any other vendor that I've ever seen, and certainly wouldn't pass a static code analysis tool like StyleCop. I'd love to see a pointer to said posts, as incorrect information like what you've mentioned here only serves to confuse and confound people. It's also important to note that FindElements() doesn't return anything custom; it's just a standard .NET Framework ReadOnlyCollection<T>, which is provided by the framework itself, and is not a special WebDriver class.

IWebElement *does* have a .Size property, which gives the dimensions (in pixels) of a specific element, but again, it's a property, and PascalCased, in keeping with .NET coding standards.

--Jim

eric aldinger

unread,
Jul 30, 2012, 4:21:33 PM7/30/12
to seleniu...@googlegroups.com
Ok. Back at a real computer.

The references in my original research are below. The first has the bad test logic > 1. The second has the size() property refereed to in the comment. The third has the syntax in the implementation details.

https://groups.google.com/forum/#!topic/selenium-users/pcBHf5gR6aw
https://groups.google.com/forum/?fromgroups#!topic/webdriver/DtcwYTw4D_E
http://stackoverflow.com/questions/9799881/using-webdriver-to-see-if-an-element-exists-without-throwing-an-exception

What I was really looking for was a way to trigger an action if a modal dialog was present, and another if it was not. I can go ahead and try a try / catch. I was being lazy and wanted the ifExists function.

Jim Evans

unread,
Jul 30, 2012, 4:46:16 PM7/30/12
to seleniu...@googlegroups.com
For what it's worth, if you read the entire thread for the first link you've given, you'd see the same information I mentioned earlier, that .size() is Java, .Count is .NET. The other two links both are explicitly showing Java code. You can usually tell in the example code because the convention for Java APIs is to use camelCased names for methods, and only methods (no properties). .NET APIs on the other hand are usually PascalCased, and may include properties and methods.

Having put that issue to rest, I'm not sure how the .FindElements() method will help you with a modal dialog, unless it's not really a modal dialog, but more like an HTML widget framework's implementation of a "lightbox", which mimics a modal dialog, but isn't really modal on the browser window.

--Jim
Reply all
Reply to author
Forward
0 new messages