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












1
















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













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();
}
);
});
});
Nov 21 '18 at 16:47


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
    Nov 21 '18 at 16:18











  • I didn't know this! Thanks. Makes sense why Denis's solution worked.

    – Isaac L
    Nov 21 '18 at 17:03
















1
















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













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();
}
);
});
});
Nov 21 '18 at 16:47


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
    Nov 21 '18 at 16:18











  • I didn't know this! Thanks. Makes sense why Denis's solution worked.

    – Isaac L
    Nov 21 '18 at 17:03














1












1








1









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















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













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 16:14









Isaac LIsaac L

82




82




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();
}
);
});
});
Nov 21 '18 at 16:47


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();
}
);
});
});
Nov 21 '18 at 16:47


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
    Nov 21 '18 at 16:18











  • I didn't know this! Thanks. Makes sense why Denis's solution worked.

    – Isaac L
    Nov 21 '18 at 17:03














  • 1





    You can't chain asynchronous calls in a forEach() loop.

    – Patrick Roberts
    Nov 21 '18 at 16:18











  • I didn't know this! Thanks. Makes sense why Denis's solution worked.

    – Isaac L
    Nov 21 '18 at 17:03








1




1





You can't chain asynchronous calls in a forEach() loop.

– Patrick Roberts
Nov 21 '18 at 16:18





You can't chain asynchronous calls in a forEach() loop.

– Patrick Roberts
Nov 21 '18 at 16:18













I didn't know this! Thanks. Makes sense why Denis's solution worked.

– Isaac L
Nov 21 '18 at 17:03





I didn't know this! Thanks. Makes sense why Denis's solution worked.

– Isaac L
Nov 21 '18 at 17:03












1 Answer
1






active

oldest

votes


















0














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
    Nov 21 '18 at 16:59


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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
    Nov 21 '18 at 16:59
















0














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
    Nov 21 '18 at 16:59














0












0








0







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 Nov 21 '18 at 16:49

























answered Nov 21 '18 at 16:34









Denis KovzelyukDenis Kovzelyuk

716




716













  • This worked!! Really great solution.

    – Isaac L
    Nov 21 '18 at 16:59



















  • This worked!! Really great solution.

    – Isaac L
    Nov 21 '18 at 16:59

















This worked!! Really great solution.

– Isaac L
Nov 21 '18 at 16:59





This worked!! Really great solution.

– Isaac L
Nov 21 '18 at 16:59





Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]