question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do one properly iterate over elements?

See original GitHub issue

On webdriver homepage there is an example:

        var results = $$('.commands.property a').filter(function (link) {
            return link.isVisible();
        });

Spectron is promised-based, therefore I adopt it to something like this:

        var results = await app.client.$$('.commands.property a')
        var filtered = results.filter(function (link) {
            return link.isVisible();
        });

But it throws TypeError: link.isVisible is not a function. Same error occurs on this:

      var results = await app.client.$$('..commands.property a')
      var isVisible = results[0].isVisible()

So what is a proper way to iterate over array of dom elelements returned from $$?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
negamaxicommented, Jun 19, 2018

Yep, it works.

0reactions
negamaxicommented, Jun 18, 2018

@DHedgecock I gues we can use :nth-child(n) for this:

const elements = await this.app.client.$$('.container > *')
const childIndexesArray = Array.from(Array(elements.length + 1).keys()).splice(1)
// [ 1, 2, 3, 4 ... ]
const childElements = await Promise.all(
  childIndexesArray.map(index => this.app.client.$(`.container > *:nth-child(${index})`)
)
// child elements must contain elements we can interact with

Haven’t tested yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Iteration over lists | AP CSP (article) - Khan Academy
To do that, we can use a loop to iterate over each element, repeating the same code for each element.
Read more >
Iterate Over Selected Elements - JavaScript Tutorial
To iterate over the selected elements, you can use forEach() method (supported by most modern web browsers, not IE) or just use the...
Read more >
How to correctly iterate through getElementsByClassName
According to MDN, the way to retrieve an item from a NodeList is: nodeItem = nodeList.item(index). Thus: var slides = document.
Read more >
JS For Loop Tutorial – How to Iterate Over an Array in JavaScript
Here's how we will tackle iterating over arrays in JavaScript: ... It moves through the array and prints one element at a time....
Read more >
HTMLCollection for Loop - GeeksforGeeks
The elements can be iterated through by using a normal for loop. The number of elements in the HTMLCollection can be found out...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found