7

I am using nightwatch and trying to iterate through a list of elements. However, when I don't get objects or elements, but I get an array of strings.

CODE

browser.elements("css selector", ele, function(r){
    browser.perform(function(){
      console.log("LIST", r);
    })
  })

RETURN

LIST { sessionId: 'b273b874-c084-4d17-8bbe-a911a170ef25',
  status: 0,
  state: 'success',
  value:
   [ { ELEMENT: '6' },
     { ELEMENT: '7' },
     { ELEMENT: '8' },
     { ELEMENT: '9' },
     { ELEMENT: '10' },
     { ELEMENT: '11' } ],
  class: 'org.openqa.selenium.remote.Response',
  hCode: 995684858 }

The value should be returning an object of webElements right?

Thanks in advance

Rogue Panda
  • 71
  • 1
  • 5

2 Answers2

10

I struggled on this one for a little bit until I went through some of the Selenium documentation.

Essentially that is what's expected to return. Those ELEMENT items are WebElement JSON objects. You can use some of the Selenium commands to try and find additional information on them doing something like this:

_.each(list.value, function(element, i){ //using underscore instead of for loop.
   browser.elementIdAttribute(element.ELEMENT, 'name', function(result){
       //result.value will contain the name attribute on the element
   }
}

You can see the list of commands on selenium here: http://nightwatchjs.org/api#protocol

Lastly, the ID that's in the documentation there is referring to that element.ELEMENT above.

Hope that helps!

Petrogad
  • 4,405
  • 5
  • 38
  • 77
  • 1
    Thank you for this!, this helped me get started on understanding how to parse the WebElement structure back from NW wire protocol. – mekdev Jun 22 '15 at 17:53
  • Not a problem! Was a bit tricky to understand from the docs out there. – Petrogad Jun 22 '15 at 18:24
-1

when I try to access a particular attribute and validate its value. The webelement chosen is different for each run.

My piece of code here

browser.elements('css selector', 'header.modal-header > h2', (results) => {
            results.value.forEach((v, k) => {
               browser.elementIdAttribute(v.ELEMENT, 'tabindex', 
                function (index) {
                   if (index.value !== '-1') {
                        browser.verify.fail(`Expected tab index -1 not found for ContactUs header ${key}`);
                   }
                });
Shiva Kishore
  • 1,611
  • 12
  • 29
Abitha
  • 1
  • It's not obvious how this answers the original poster's question. You might want to edit your posting to make that more obvious (and check out https://stackoverflow.com/help/how-to-answer). – rwp Apr 02 '18 at 18:45