const scrapedContacts = new Set();
const contactList = [];
const scrollSelector = 'div[aria-label="Chat list"]';
let currentScroll = 0;
let targetScroll = await page.evaluate((sel) => {
const el = document.querySelector(sel);
return el?.scrollHeight || 0;
}, scrollSelector);
while (currentScroll < targetScroll ) {
const isRunning = await page.evaluate(() => window.scrapeRunning);
if (!isRunning) {
console.log("
break;
}
// Refetch chat items (they change due to lazy loading)
const chatBoxes = await page.$$(
'#pane-side [aria-label="Chat list"] > div > div'
);
for (let i = 0; i < chatBoxes.length; i++) {
console.log(`Length: ${i}/${chatBoxes.length}`);
const chat = chatBoxes;
// Get height of the current chat
const chatHeight = await chat.evaluate((el) => el.offsetHeight);
try {
await chat.scrollIntoViewIfNeeded();
await chat.click();
await page.waitForTimeout(500);
await page.click('[title="Profile details"]');
// Skip groups/communities
const isGroup = await page.$(
'div[title="Group info"], div[title="Community info"]'
);
if (isGroup) {
console.log("
await page.evaluate((scrollBy) => {
const el = document.querySelector("#pane-side");
if (el) el.scrollBy(0, scrollBy);
}, chatHeight);
continue;
}
await page.waitForSelector('div[title="Contact info"]', {
timeout: 5000,
});
const info = await page.evaluate(() => {
const nameEl = document.querySelector('header span[dir="auto"]');
const numberEl = document.querySelector(
'span[dir="auto"][class*="_ao3e"] > span'
);
const name = nameEl?.textContent || "";
const number = numberEl?.textContent || "";
return {
name: /^\+\d+/.test(name) ? "" : name,
number: /^\+\d+/.test(name) ? name : number,
};
});
const key = `${info.name}-${info.number}`;
if (!scrapedContacts.has(key) && info.number) {
scrapedContacts.add(key);
contactList.push(info);
console.log("
}
} catch (err) {
console.log("
}
// Scroll down by the height of the current chat
await page.evaluate((scrollBy) => {
const el = document.querySelector("#pane-side");
if (el) el.scrollBy(0, scrollBy);
}, chatHeight);
currentScroll += chatHeight;
}
await page.waitForTimeout(1000);
}
< /code>
Was ich ausprobiert habe: < /h2>
- Scrolling #Pane-Side mit ScrollBy () < /li>
Verwenden Sie bei jedem Chat. Liste nach jedem Scroll
Jede Hilfe mit einer besseren Strategie, um alle Kontakte von WhatsApp -Web mit Dramatikern zuverlässig zu scrollen und zu holen, würde geschätzt!>