Skip to content

Instantly share code, notes, and snippets.

@TomyCesaille
Created April 3, 2020 15:12
Show Gist options
  • Save TomyCesaille/3cd806ac93d1dfef2ce1ce4204519085 to your computer and use it in GitHub Desktop.
Save TomyCesaille/3cd806ac93d1dfef2ce1ce4204519085 to your computer and use it in GitHub Desktop.
puppeteer status scraping on WhatsApp web
const puppeteer = require('puppeteer');
// The contact name to track (mind the case).
const contactTarget = "Jean-Mich";
(async () => {
const browser = await puppeteer.launch({
headless: false, // No headless to scan the QR code.
userDataDir: 'data/userdata' // Persist the session.
});
const page = await browser.newPage();
await page.goto('https://web.whatsapp.com/');
await page.waitFor(5000);
console.log('Awaiting/Checking peering with WhatsApp phone');
await page.waitFor('#side', { timeout: 60000 }).then(() => { // Scan the QR code within the next minute.
console.log('Connected !');
}).catch((res) => {
console.log('Not connected !', res);
return -1;
})
await page.waitFor(1000);
await page.focus('._2S1VP'); // Focus search input form.
await page.keyboard.type(contactTarget, { delay: 100 });
await page.waitFor(6000);
let contactElt = (await page.$x(`//*[@class="_25Ooe" and . = "${contactTarget}"]`))[0]; // Select the best result.
contactElt.click();
await page.waitFor(5000);
let statusElt = await page.$('.O90ur');
let status = await statusElt.evaluate(x => x.textContent);
console.log(`Status for ${contactTarget} is '${status}'.`); // `last seen today at 13:15` format.
await browser.close();
})();
@ritika-rustagi
Copy link

how are u entering the qr code can u plz elaborate

@TomyCesaille
Copy link
Author

how are u entering the qr code can u plz elaborate

@ritika-rustagi this code saves a screenshot in the file system. You are supposed to open this screenshot and flash the QR code it contains with your Whats-app mobile app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment