Get all element attributes using protractor

后端 未结 4 762
故里飘歌
故里飘歌 2020-12-17 15:54

According to the documentation, to get a single attribute by name you can use .getAttribute() on a WebElement:

var myElement = element(by.id(\'m         


        
4条回答
  •  再見小時候
    2020-12-17 16:20

    You have to use browser.executeScript() function call instead of protractor API since Element.attributes is out of protractor API implementation:

    var elem = element(by.id('runButton'));
    browser.executeScript("return arguments[0].attributes", elem.getWebElement())
        .then(function (attrs) {
            console.log(attrs.length);    // outputs numbers of attributes.
            // access collection of Attr objects
            console.log(attrs[0].isId);   // outputs `true`
            console.log(attrs[0].name);   // outputs `id`
            console.log(attrs[0].value);  // outputs `runButton`
        });
    

    Remember that when saying attributes, it means a named map structure instead an array in the context of DOM model. Which means you have to use the NamedNodeMap to access collection of Attr objects.

    It works as the same way as that in @alecxe's answer without the iteration part.

提交回复
热议问题