How to iterate through querySelector All results in Nightwatch

I cannot find the correct syntax to iterate through the innerHTML nodelist in Nightwatch. I am trying to return the URLs of each 'a' tag contained in the contents of the page body, but I cannot find a way to access the results of my querySelectorAll command in Nightwatch.

browser.execute(function() { return document.querySelectorAll("div.field-item.even a"); }, function(tags){ console.log(tags.value); console.log(tags.value[9]); }) 

There are 10 links on the page I'm testing. The query selector seems to retrieve all of them, since console.log (tags.value) prints the following:

 [ { ELEMENT: '1' }, { ELEMENT: '2' }, { ELEMENT: '3' }, { ELEMENT: '4' }, { ELEMENT: '5' }, { ELEMENT: '6' }, { ELEMENT: '7' }, { ELEMENT: '8' }, { ELEMENT: '9' }, { ELEMENT: '10' } ] 

and console.log (tags.value [9]) prints:

  { ELEMENT: '10' } 

However, I cannot find a way to get the link from these results. Adding .innerHTML to any of these variables returns 'undefined'. Is there any way to iterate through the results of querySelectorAll and extract the urls inside it?

EDIT: It seems that I can get the same result if I use the following code:

 browser.elements("css selector", "div.field-item.even a", function(tags) { console.log(tags.value); console.log(tags.value[9]); }) 

I initially thought that I was working with a nodelist, but I think that in fact they return a WebElement JSON object to me according to the documentation for the .elements command .

I still canโ€™t access the inner text. Any ideas?

+5
source share
2 answers

I solved the problem using a different method. I used the browser.elements command instead of querySelectorAll and then used the browser. elementIdAttribute to get the contents of each href attribute in each a tag.

  //Get an WebElement JSON object array of all urls browser.elements("css selector", "div.field-item.even a", function(link_array) { for (var x = 0; x < link_array.value.length; x++){ //Fetch the url from each 'a' tag in the content browser.elementIdAttribute(link_array.value[x].ELEMENT, "href", function(links) { console.log(links.value); }); } }) 

This prints each link in the content of the page.

+3
source
 selectAllRoles: function (client) { client.elements('css selector', '.editorDetail ul li input[type=checkbox]', function(res) { res.value.forEach(elementObject => { client.elementIdClick(elementObject.ELEMENT); }); }); return this; }, 

Fill in your id after "css selector"

0
source

Source: https://habr.com/ru/post/1235624/


All Articles