admin管理员组文章数量:1026989
I need a way to add some delay between clicking elements inside a page.evaluate function
below is what ive tried and it does not work
const result = await page.evaluate(async () => {
let variants = document.querySelectorAll(".item-sku-image a");
variants.forEach(async variant => {
await new Promise(function(resolve) {
setTimeout(resolve, 1000)
});
await variant.click()
});
return data
});
Update:
the for loop below works fine for one element when i try to use another for loop inside it - it goes crazy
const variants = [...document.querySelectorAll(".item-sku-image a")]; // Turn nodelist into an array
const sizes = [...document.querySelectorAll(".item-sku-size a")]; // Turn nodelist into an array
for (let variant of variants){
// wait one second
await new Promise(function(resolve) {setTimeout(resolve, 1000)});
await variant.click()
for (let size of sizes){
// wait one second
await new Promise(function(resolve) {setTimeout(resolve, 1000)});
await size.click()
}
}
I need a way to add some delay between clicking elements inside a page.evaluate function
below is what ive tried and it does not work
const result = await page.evaluate(async () => {
let variants = document.querySelectorAll(".item-sku-image a");
variants.forEach(async variant => {
await new Promise(function(resolve) {
setTimeout(resolve, 1000)
});
await variant.click()
});
return data
});
Update:
the for loop below works fine for one element when i try to use another for loop inside it - it goes crazy
const variants = [...document.querySelectorAll(".item-sku-image a")]; // Turn nodelist into an array
const sizes = [...document.querySelectorAll(".item-sku-size a")]; // Turn nodelist into an array
for (let variant of variants){
// wait one second
await new Promise(function(resolve) {setTimeout(resolve, 1000)});
await variant.click()
for (let size of sizes){
// wait one second
await new Promise(function(resolve) {setTimeout(resolve, 1000)});
await size.click()
}
}
Share
Improve this question
edited May 14, 2019 at 15:01
Limpfro
asked May 13, 2019 at 21:09
LimpfroLimpfro
1776 silver badges12 bronze badges
1 Answer
Reset to default 5.forEach
will not work with the promises or async...await
the way you want.
Use for..of
instead.
const variants = [...document.querySelectorAll(".item-sku-image a")]; // Turn nodelist into an array
for (let variant of variants){
// wait one second
await new Promise(function(resolve) {setTimeout(resolve, 1000)});
await variant.click()
}
It's easy to read, understand and implement.
Here are some references:
- https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/for...of
- https://stackoverflow./a/37576787/6161265
I need a way to add some delay between clicking elements inside a page.evaluate function
below is what ive tried and it does not work
const result = await page.evaluate(async () => {
let variants = document.querySelectorAll(".item-sku-image a");
variants.forEach(async variant => {
await new Promise(function(resolve) {
setTimeout(resolve, 1000)
});
await variant.click()
});
return data
});
Update:
the for loop below works fine for one element when i try to use another for loop inside it - it goes crazy
const variants = [...document.querySelectorAll(".item-sku-image a")]; // Turn nodelist into an array
const sizes = [...document.querySelectorAll(".item-sku-size a")]; // Turn nodelist into an array
for (let variant of variants){
// wait one second
await new Promise(function(resolve) {setTimeout(resolve, 1000)});
await variant.click()
for (let size of sizes){
// wait one second
await new Promise(function(resolve) {setTimeout(resolve, 1000)});
await size.click()
}
}
I need a way to add some delay between clicking elements inside a page.evaluate function
below is what ive tried and it does not work
const result = await page.evaluate(async () => {
let variants = document.querySelectorAll(".item-sku-image a");
variants.forEach(async variant => {
await new Promise(function(resolve) {
setTimeout(resolve, 1000)
});
await variant.click()
});
return data
});
Update:
the for loop below works fine for one element when i try to use another for loop inside it - it goes crazy
const variants = [...document.querySelectorAll(".item-sku-image a")]; // Turn nodelist into an array
const sizes = [...document.querySelectorAll(".item-sku-size a")]; // Turn nodelist into an array
for (let variant of variants){
// wait one second
await new Promise(function(resolve) {setTimeout(resolve, 1000)});
await variant.click()
for (let size of sizes){
// wait one second
await new Promise(function(resolve) {setTimeout(resolve, 1000)});
await size.click()
}
}
Share
Improve this question
edited May 14, 2019 at 15:01
Limpfro
asked May 13, 2019 at 21:09
LimpfroLimpfro
1776 silver badges12 bronze badges
1 Answer
Reset to default 5.forEach
will not work with the promises or async...await
the way you want.
Use for..of
instead.
const variants = [...document.querySelectorAll(".item-sku-image a")]; // Turn nodelist into an array
for (let variant of variants){
// wait one second
await new Promise(function(resolve) {setTimeout(resolve, 1000)});
await variant.click()
}
It's easy to read, understand and implement.
Here are some references:
- https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/for...of
- https://stackoverflow./a/37576787/6161265
本文标签: javascriptPuppeteerTimeout or sleep or wait inside PageEvaluate()Stack Overflow
版权声明:本文标题:javascript - Puppeteer : Timeout or sleep or wait inside Page.Evaluate() - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745666906a2162228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论