javascript-> webdriver.io中的getAttribute

时间:2018-12-14 15:14:44

标签: javascript webdriver-io

    var checkBoxes = await $$('.checkbox.moduleName') 
    console.log('length', $$('.checkbox.moduleName').length)
    checkBoxes.forEach((elem)=>{
        console.log(elem)
        console.log(elem.getAttribute('value'))
    })

这是我的代码。我有一些带有类'.checkbox.moduleName'的复选框。当我打印长度时,它将被打印为10。但是getAttribute函数不起作用。

 TypeError: elem.getAttribute is not a function
at checkBoxes.forEach 

如何获取复选框的“值”属性?

我也尝试过

       var valueAttrs =
        checkBoxes
            .map(function(e){
                console.log('e', e)
                return e.getAttribute('value');
            });

    valueAttrs.forEach(v=>{console.log('value: ' + v);});

相同错误

这是我拥有的html行

  <input type="checkbox" class="checkbox moduleName" id="module110" name="module[]" value="110">

1 个答案:

答案 0 :(得分:2)

elem将持有一个ELEMENT对象,因此您必须基于ELEMENT值通过 Protocol API 命令获取属性( id ),在我们的情况下为elementIdAttribute

因此,请尝试以下操作:

var checkBoxes = await $$('.checkbox.moduleName');
console.log('length', $$('.checkbox.moduleName').length);

checkBoxes.forEach((elem) => {
    console.log(elem);
    console.log(browser.getIdAttribute(elem.value.ELEMENT, 'value'));
});

示例用于带有登录表单的页面):

> let tt = $$('form input');
> tt
  

[{ELEMENT:'0.41525845584401401533-2',       'element-6066-11e4-a52e-4f735466cecf':'0.41525845584401401533-2',       选择器:“表单输入”,       值:{ELEMENT:'0.41525845584401533-2'},       索引:0},{元素:“ 0.41525845584401401533-3”,       'element-6066-11e4-a52e-4f735466cecf':'0.41525845584401401533-3',       选择器:“表单输入”,       值:{ELEMENT:'0.41525845584401533-3'},       索引:1},{ELEMENT:“ 0.41525845584401533-4”,       'element-6066-11e4-a52e-4f735466cecf':'0.41525845584401533-4',       选择器:“表单输入”,       值:{ELEMENT:'0.41525845584401533-4'},       索引:2},{元素:'0.41525845584401533-5',       'element-6066-11e4-a52e-4f735466cecf':'0.41525845584401533-5',       选择器:“表单输入”,       值:{ELEMENT:'0.41525845584401533-5'},       索引:3}]

> tt.forEach((elem) => { console.log(browser.elementIdAttribute(elem.value.ELEMENT, 'class')); })
  

{sessionId:'1eb7b6df6484f97a9571bb93d6a95d1d',值:'_2hvTZpexuQ zyHYP',_ status:0}   {sessionId:'1eb7b6df6484f97a9571bb93d6a95d1d',值:'_2hvTZ pexuQ zyHYP',_ status:0} {sessionId:'1eb7b6df6484f97a9571bb93d6a95d1d',值:'_2hvTZYstatt {{   '1eb7b6df6484f97a9571bb93d6a95d1d',值:'_ 2hvTZ pexuQ zyHYP',_ status:0}

您可以通过value属性进一步使用该属性值:

tt.forEach((elem) => { 
  let ret = browser.elementIdAttribute(elem.value.ELEMENT, 'class'); 
  console.log(`> Elem has these classes: ${ret.value}`); 
});
  

Elem具有以下类别:_2hvTZ pexuQ zyHYP   Elem具有这些类别:_2hvTZ pexuQ zyHYP   Elem具有这些类别:_2hvTZ pexuQ zyHYP   Elem具有这些类别:_2hvTZ pexuQ zyHYP