Loop inside a loop
See original GitHub issueI have this this code, first part is running ok but when it´s time to pass the i value to second loop, it doesn´t work. It looks like that:
function () {
return document.querySelectorAll('.menuElementsAgregator>li:nth-of-type(' + i + ')>.tsr-nav-second-level .has-sub .clickableTabWithLink').length
},
doesn´t accept(' + i + ') because if for eg. i manually write 2 in place of i rest of the code will run
.execute(
function() {
return document.querySelectorAll('.menuElementsAgregator>li').length
},
function(result) {
total_links = result.value;
console.log("Number of main links:" + total_links);
for (var i = 2; i <= total_links; i++) {
(function (i) {
browser.waitForElementPresent('.menuElementsAgregator', 3000)
.click('.menuElementsAgregator>li:nth-child(' + i + ')>a')
.waitForElementVisible('.menuElementsAgregator>li:nth-child(' + i + ')', 2000)
.execute(
function () {
return document.querySelectorAll('.menuElementsAgregator>li:nth-of-type(' + i + ')>.tsr-nav-second-level .has-sub .clickableTabWithLink').length
},
function(result) {
total_links2 = result.value;
console.log("Number of links:" + total_links2);
for (var j = 2; j <= total_links2 + 1; j++) {
browser.waitUntilElementIsClickable('.menuElementsAgregator>li:nth-child(' + i + ')')
.click('.menuElementsAgregator>li:nth-child(' + i + ')')
.waitForElementPresent('.menuElementsAgregator>li:nth-of-type(' + i + ')>.tsr-nav-second-level>li:nth-of-type(' + j + ').has-sub', 5000)
.click(' .menuElementsAgregator>li:nth-of-type(' + i + ')>.tsr-nav-second-level>li:nth-of-type(' + j + ').has-sub .clickableTabWithLink:first-child')
.pause(1000)
.waitForElementVisible('.games-list', 5000);
// .end();
}
}
)
})(i);
}
})
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
11.2. Nested Loops
A nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is...
Read more >4.4. Nested For Loops — AP CSAwesome
A nested loop has one loop inside of another. These are typically used for working with two dimensions such as printing stars in...
Read more >Nested Loops in Python - PYnative
A nested loop is a loop inside the body of the outer loop. The inner or outer loop can be any type, such...
Read more >Nested Loop in Java (With Examples) - Programiz
In this tutorial, we will learn about nested loops in Java with the help of examples. If a loop exists inside the body...
Read more >6.6: Nested Loops - Processing Tutorial - YouTube
This video looks at nested loops, i.e. a loop inside a loop.Support this channel on Patreon: https://patreon.com/codingtrainContact: ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Ok, so the answer here is that you cannot refer to variables within the execute first function as you would normally expect to. What you can do, is make use of the optional second argument to the
executefunction, which allows you to pass in data. So in your case, you can pass in the value ofidoing something like the following:Note the second argument which is an array of data that will be passed into the execute function, referred to in order by the function arguments. You may want to rename
iwithin the initial function, to avoid future confusion - but that should work for you.See: http://nightwatchjs.org/api#execute
In the future please post requests for assistance on the Mailing List or StackOverflow.