admin管理员组文章数量:1405147
I am having trouble recording an element on a page after checking if it exists. The block of code I'm referring to is under the "// phone" ment.
This code loops through each section (section of sections) on the page and records "pany" and "phone." "Phone" may not be present in some sections so I figured I'd pass it through an if statement to check if it exists. This creates an error = "Error: failed to find element matching selector ".mn-contact-phone"" How do I solve this?
(async () => {
try {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
// loop through pages
for (let pg = 1; pg < 5; pg++) {
await page.goto("webpage");
// record number of sections
const sections = await page.$$("#mn-members-listings > div");
// loop through each section
for (const section of sections) {
// pany
let pany = await section.$eval(
"div.mn-searchlisting-title",
p => p.innerText
);
// phone --> THIS IF/ELSE THROWS AN ERROR
if (section.$(".mn-contact-phone").length > 0) {
let phone = await section.$eval(".mn-contact-phone", phn => phn.innerText);
} else {
let phone = "";
}
console.log(`Company = ${pany} | Phone = ${phone}`);
}
}
await browser.close();
} catch (error) {
console.log(`Our error is = ${error}`);
}
})();
I am having trouble recording an element on a page after checking if it exists. The block of code I'm referring to is under the "// phone" ment.
This code loops through each section (section of sections) on the page and records "pany" and "phone." "Phone" may not be present in some sections so I figured I'd pass it through an if statement to check if it exists. This creates an error = "Error: failed to find element matching selector ".mn-contact-phone"" How do I solve this?
(async () => {
try {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
// loop through pages
for (let pg = 1; pg < 5; pg++) {
await page.goto("webpage");
// record number of sections
const sections = await page.$$("#mn-members-listings > div");
// loop through each section
for (const section of sections) {
// pany
let pany = await section.$eval(
"div.mn-searchlisting-title",
p => p.innerText
);
// phone --> THIS IF/ELSE THROWS AN ERROR
if (section.$(".mn-contact-phone").length > 0) {
let phone = await section.$eval(".mn-contact-phone", phn => phn.innerText);
} else {
let phone = "";
}
console.log(`Company = ${pany} | Phone = ${phone}`);
}
}
await browser.close();
} catch (error) {
console.log(`Our error is = ${error}`);
}
})();
Share
Improve this question
edited Jan 19, 2020 at 18:25
Rtroman14
asked Jan 19, 2020 at 16:57
Rtroman14Rtroman14
3283 silver badges11 bronze badges
1 Answer
Reset to default 4From puppeteer docs:
The method runs
document.querySelector
within the page. If no element matches the selector, the return value resolves tonull
.
1) null
doesn't have length.
2) ElementHandle.$
returns a promise.
change the condition to:
if (await section.$(".mn-contact-phone"))
or if there are multiple elements:
if (await section.$$(".mn-contact-phone").length > 0)
本文标签: javascriptHow to check if element exists inside asyncawait with PuppeteerStack Overflow
版权声明:本文标题:javascript - How to check if element exists inside asyncawait with Puppeteer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744885795a2630476.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论