Selenium in Javascript - Having trouble looping through multiple sites: sites.forEach(site => {...
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 => {
})
})
})
javascript selenium testing
marked as duplicate by Jonas Wilms
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.
add a comment |
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 => {
})
})
})
javascript selenium testing
marked as duplicate by Jonas Wilms
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 aforEach()
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
add a comment |
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 => {
})
})
})
javascript selenium testing
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
javascript selenium testing
asked Nov 21 '18 at 16:14
Isaac LIsaac L
82
82
marked as duplicate by Jonas Wilms
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
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 aforEach()
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
add a comment |
1
You can't chain asynchronous calls in aforEach()
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
add a comment |
1 Answer
1
active
oldest
votes
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 );
This worked!! Really great solution.
– Isaac L
Nov 21 '18 at 16:59
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 );
This worked!! Really great solution.
– Isaac L
Nov 21 '18 at 16:59
add a comment |
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 );
This worked!! Really great solution.
– Isaac L
Nov 21 '18 at 16:59
add a comment |
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 );
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 );
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
add a comment |
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
add a comment |
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