Selenium in Javascript - Having trouble looping through multiple sites: sites.forEach(site => {...











up vote
1
down vote

favorite













This question already has an answer here:




  • Using async/await with a forEach loop

    12 answers




I think I setup my driver.get(site) calls wrong as they're all executing at the same time. I'm trying to have selenium visit a site, do some logic, pause for a bit, then start over by visiting a different site. Unfortunately, this is all happening at the same time right now, causing the browser to redirect to the next site in the array as soon as the page loads.



let webdriver = require("selenium-webdriver");
let sites = [
'https://www.site1.com'
, 'https://www.site2.com'
, 'https://www.site3.com'
];
let driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();
sites.forEach(site => {
driver.get(site).then(x => {
// perform a mixture of send keys, clicks, and a mandatory delay
Promise.all([
promise1
, promise2
, driver.sleep(1000)
]).then(y => {

}).catch(err => {

})
})
})









share|improve this question







New contributor




Isaac L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Jonas Wilms javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
14 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    You can't chain asynchronous calls in a forEach() loop.
    – Patrick Roberts
    15 hours ago










  • I didn't know this! Thanks. Makes sense why Denis's solution worked.
    – Isaac L
    14 hours ago















up vote
1
down vote

favorite













This question already has an answer here:




  • Using async/await with a forEach loop

    12 answers




I think I setup my driver.get(site) calls wrong as they're all executing at the same time. I'm trying to have selenium visit a site, do some logic, pause for a bit, then start over by visiting a different site. Unfortunately, this is all happening at the same time right now, causing the browser to redirect to the next site in the array as soon as the page loads.



let webdriver = require("selenium-webdriver");
let sites = [
'https://www.site1.com'
, 'https://www.site2.com'
, 'https://www.site3.com'
];
let driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();
sites.forEach(site => {
driver.get(site).then(x => {
// perform a mixture of send keys, clicks, and a mandatory delay
Promise.all([
promise1
, promise2
, driver.sleep(1000)
]).then(y => {

}).catch(err => {

})
})
})









share|improve this question







New contributor




Isaac L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Jonas Wilms javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
14 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    You can't chain asynchronous calls in a forEach() loop.
    – Patrick Roberts
    15 hours ago










  • I didn't know this! Thanks. Makes sense why Denis's solution worked.
    – Isaac L
    14 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite












This question already has an answer here:




  • Using async/await with a forEach loop

    12 answers




I think I setup my driver.get(site) calls wrong as they're all executing at the same time. I'm trying to have selenium visit a site, do some logic, pause for a bit, then start over by visiting a different site. Unfortunately, this is all happening at the same time right now, causing the browser to redirect to the next site in the array as soon as the page loads.



let webdriver = require("selenium-webdriver");
let sites = [
'https://www.site1.com'
, 'https://www.site2.com'
, 'https://www.site3.com'
];
let driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();
sites.forEach(site => {
driver.get(site).then(x => {
// perform a mixture of send keys, clicks, and a mandatory delay
Promise.all([
promise1
, promise2
, driver.sleep(1000)
]).then(y => {

}).catch(err => {

})
})
})









share|improve this question







New contributor




Isaac L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












This question already has an answer here:




  • Using async/await with a forEach loop

    12 answers




I think I setup my driver.get(site) calls wrong as they're all executing at the same time. I'm trying to have selenium visit a site, do some logic, pause for a bit, then start over by visiting a different site. Unfortunately, this is all happening at the same time right now, causing the browser to redirect to the next site in the array as soon as the page loads.



let webdriver = require("selenium-webdriver");
let sites = [
'https://www.site1.com'
, 'https://www.site2.com'
, 'https://www.site3.com'
];
let driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();
sites.forEach(site => {
driver.get(site).then(x => {
// perform a mixture of send keys, clicks, and a mandatory delay
Promise.all([
promise1
, promise2
, driver.sleep(1000)
]).then(y => {

}).catch(err => {

})
})
})




This question already has an answer here:




  • Using async/await with a forEach loop

    12 answers








javascript selenium testing






share|improve this question







New contributor




Isaac L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Isaac L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Isaac L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 15 hours ago









Isaac L

82




82




New contributor




Isaac L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Isaac L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Isaac L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




marked as duplicate by Jonas Wilms javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
14 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jonas Wilms javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
14 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    You can't chain asynchronous calls in a forEach() loop.
    – Patrick Roberts
    15 hours ago










  • I didn't know this! Thanks. Makes sense why Denis's solution worked.
    – Isaac L
    14 hours ago














  • 1




    You can't chain asynchronous calls in a forEach() loop.
    – Patrick Roberts
    15 hours ago










  • I didn't know this! Thanks. Makes sense why Denis's solution worked.
    – Isaac L
    14 hours ago








1




1




You can't chain asynchronous calls in a forEach() loop.
– Patrick Roberts
15 hours ago




You can't chain asynchronous calls in a forEach() loop.
– Patrick Roberts
15 hours ago












I didn't know this! Thanks. Makes sense why Denis's solution worked.
– Isaac L
14 hours ago




I didn't know this! Thanks. Makes sense why Denis's solution worked.
– Isaac L
14 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Use recursive approach and call the function from .then(y => {})



let webdriver = require("selenium-webdriver");
let sites = [
'https://www.site1.com'
, 'https://www.site2.com'
, 'https://www.site3.com'
];
let driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();

function doCheck( sites, ind ) {
driver.get(sites[ind]).then(x => {
// perform a mixture of send keys, clicks, and a mandatory delay
Promise.all([
promise1
, promise2
, driver.sleep(1000)
]).then(y => {
if (sites.indexOf(sites[ind++]) !== -1){
doCheck(sites, ind++);
}
}).catch(err => {

})
})
}

doCheck( sites, 0 );





share|improve this answer























  • This worked!! Really great solution.
    – Isaac L
    14 hours ago


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Use recursive approach and call the function from .then(y => {})



let webdriver = require("selenium-webdriver");
let sites = [
'https://www.site1.com'
, 'https://www.site2.com'
, 'https://www.site3.com'
];
let driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();

function doCheck( sites, ind ) {
driver.get(sites[ind]).then(x => {
// perform a mixture of send keys, clicks, and a mandatory delay
Promise.all([
promise1
, promise2
, driver.sleep(1000)
]).then(y => {
if (sites.indexOf(sites[ind++]) !== -1){
doCheck(sites, ind++);
}
}).catch(err => {

})
})
}

doCheck( sites, 0 );





share|improve this answer























  • This worked!! Really great solution.
    – Isaac L
    14 hours ago















up vote
0
down vote



accepted










Use recursive approach and call the function from .then(y => {})



let webdriver = require("selenium-webdriver");
let sites = [
'https://www.site1.com'
, 'https://www.site2.com'
, 'https://www.site3.com'
];
let driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();

function doCheck( sites, ind ) {
driver.get(sites[ind]).then(x => {
// perform a mixture of send keys, clicks, and a mandatory delay
Promise.all([
promise1
, promise2
, driver.sleep(1000)
]).then(y => {
if (sites.indexOf(sites[ind++]) !== -1){
doCheck(sites, ind++);
}
}).catch(err => {

})
})
}

doCheck( sites, 0 );





share|improve this answer























  • This worked!! Really great solution.
    – Isaac L
    14 hours ago













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Use recursive approach and call the function from .then(y => {})



let webdriver = require("selenium-webdriver");
let sites = [
'https://www.site1.com'
, 'https://www.site2.com'
, 'https://www.site3.com'
];
let driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();

function doCheck( sites, ind ) {
driver.get(sites[ind]).then(x => {
// perform a mixture of send keys, clicks, and a mandatory delay
Promise.all([
promise1
, promise2
, driver.sleep(1000)
]).then(y => {
if (sites.indexOf(sites[ind++]) !== -1){
doCheck(sites, ind++);
}
}).catch(err => {

})
})
}

doCheck( sites, 0 );





share|improve this answer














Use recursive approach and call the function from .then(y => {})



let webdriver = require("selenium-webdriver");
let sites = [
'https://www.site1.com'
, 'https://www.site2.com'
, 'https://www.site3.com'
];
let driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();

function doCheck( sites, ind ) {
driver.get(sites[ind]).then(x => {
// perform a mixture of send keys, clicks, and a mandatory delay
Promise.all([
promise1
, promise2
, driver.sleep(1000)
]).then(y => {
if (sites.indexOf(sites[ind++]) !== -1){
doCheck(sites, ind++);
}
}).catch(err => {

})
})
}

doCheck( sites, 0 );






share|improve this answer














share|improve this answer



share|improve this answer








edited 14 hours ago

























answered 15 hours ago









Denis Kovzelyuk

244




244












  • This worked!! Really great solution.
    – Isaac L
    14 hours ago


















  • This worked!! Really great solution.
    – Isaac L
    14 hours ago
















This worked!! Really great solution.
– Isaac L
14 hours ago




This worked!! Really great solution.
– Isaac L
14 hours ago



Popular posts from this blog

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)