5

Suppose my html is like so

<select class="list">
  <option val="00"></option>
  <option val="01">One</option>
</select>

This js test file is able to run, but I am trying to get the number of children elements in the select list.

var assert = require('assert'),
  test = require('selenium-webdriver/testing'),
  webdriver = require('selenium-webdriver');

var demoFile = '/path/to/my/test.html';
driver.get(demoFile);

//Setup driver
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).build();

//Get the child elements of select list, which are the options
var ele = driver.findElement(webdriver.By.className('list'))
  .findElements(webdriver.By.tagName('option')));

//size is undefined
ele.size();

However, I am get the error below when I try to get the option count.

TypeError: Object [object Object] has no method 'size'
wklhuey
  • 191
  • 1
  • 1
  • 9

3 Answers3

3

I found out that a 'then' callback, works to find the number of elements from this question

Selecting nested iframe - selenium / javascript / node-js

driver.findElement(webdriver.By.className('list'))
  .findElements(webdriver.By.tagName('option')))
  .then(function(elements){
    console.log(elements.length);
});
Community
  • 1
  • 1
wklhuey
  • 191
  • 1
  • 1
  • 9
0

You can use the Select functionality:

Select select = new Select(driver.findElement(webdriver.By.className('list')));
List<WebElement> listOptions = select.getOptions();
listOptions.size();
Richard
  • 8,961
  • 3
  • 38
  • 47
  • Thanks for helping, but the selenium-webdriver in nodejs does not have the package to allow for the Select class to be used. – wklhuey May 23 '14 at 02:17
0

findElement just returns single webElement . should use findElements which return List of webElements

kiran
  • 131
  • 1
  • 4