量角器获取下拉列表中所有项目的数量

时间:2019-04-09 04:35:00

标签: javascript protractor

我正在尝试获取下拉列表中所有选项的数量。下面是我认为应该可以使用的代码,但是它不起作用

let elements_count  = await $$('#directClassBased > option').count();
console.log('\nThe length of the elements are'+ await elements_count+' \n');

代码将计数返回为0。如何解决?

与保管箱相关的html如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><select _ngcontent-c16="" class="form-control ng-dirty ng-valid ng-touched" formcontrolname="dcbsId" id="directClassBased">
        <!---->
        <option _ngcontent-c16="" value="0: C92AB645-9485-4A15-8F49-5412D1639962">Addison</option>
        <option _ngcontent-c16="" value="1: A85A556D-ABCA-44E5-80A5-9AF313E161FC">Dundalk</option>
        ....
        ....
    </select></title>
</head>
<body>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

尝试以下方式

const elements_count  = element.all(by.css('#directClassBased > option'));
const drop = element(by.css('#directClassBased'));

await drop.click(); // to open the drop down 
await browser.sleep(1000);
console.log('\nThe length of the elements are'+ await elements_count.count() +' \n');