wklhuey
wklhuey

Reputation: 311

Selenium-webdriver get Children Element Count in JavaScript

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'

Upvotes: 6

Views: 12843

Answers (3)

kiran
kiran

Reputation: 141

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

Upvotes: 0

wklhuey
wklhuey

Reputation: 311

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);
});

Upvotes: 4

Richard
Richard

Reputation: 9019

You can use the Select functionality:

Select select = new Select(driver.findElement(webdriver.By.className('list')));
List<WebElement> listOptions = select.getOptions();
listOptions.size();

Upvotes: 0

Related Questions