Skip to content

Commit

Permalink
Handle "Accept Cookies" button (#8)
Browse files Browse the repository at this point in the history
Handle "Accept Cookies" button
Added a step to handle that button and also notify the user when login fails. Also adjusted the format.
  • Loading branch information
Artotim authored Mar 30, 2022
1 parent 5e6b102 commit db92cd5
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,60 @@ async function crawl() {
});
const page = await browser.newPage();


console.log(`Loading ${LOGIN_PAGE}`);
await page.goto(LOGIN_PAGE).then(() => {
console.log(' -> success!')
console.log(' -> success!');
});


console.log('Filling login form with credentials');
await page.type('#email', conf.login);
await page.type('#password', conf.password);
await Promise.all([
page.click('#submit-button'),
page.waitForNavigation({waitUntil: 'networkidle0'})
]).then(() => {
console.log(' -> success!')
});
await page.click('.grid--cell.s-btn.s-btn__primary.js-accept-cookies.js-consent-banner-hide')
.catch(e => console.log("Could not find 'Accept Cookies' button."));
await page.waitFor(2000);

try {
await Promise.all([
page.click('#submit-button'),
page.waitForNavigation({waitUntil: 'networkidle0'})
]).then(() => {
console.log(' -> success!');
});
} catch (error) {
console.error(error);
notify(`Login error. Could not login to StackOverflow.`);
await browser.close();
return;
}


console.log('Loading profile page');
await page.goto(PROFILE_PAGE).then(() => {
console.log(' -> success!')
console.log(' -> success!');
});

try {
console.log('Grabbing stats');
await page.waitForSelector(statsSelector = '#top-cards span.fs-caption', { timeout: 5000 })
.then(() => {
console.log(' -> success!')
});
await page.waitForSelector(statsSelector = '#top-cards span.fs-caption', {timeout: 5000})
.then(() => {
console.log(' -> success!');
});

const stats = await page.evaluate(
statsSelector => document.querySelector(statsSelector).textContent,
statsSelector
);
statsSelector => document.querySelector(statsSelector).textContent,
statsSelector
);

console.log(`Sending stats: ${stats}`);
notify(`Logged in for ${stats} consecutive days`)
console.log(`Sending stats: ${stats}`);
notify(`Logged in for ${stats} consecutive days`)

} catch(e) {
console.log(' -> failed. Could not fetch the stats. Please check days left manually.')
} catch (e) {
console.log(' -> failed. Could not fetch the stats. Please check days left manually.');
notify(`Login succeeded. Could not fetch stats for consecutive days.`)
}


console.log('Closing browser');
await browser.close();
Expand All @@ -84,10 +98,9 @@ async function crawl() {
function job() {
try {
crawl();
}
catch (error) {
} catch (error) {
console.error(error);
notify(`Something went wrong: ${error.toString()}`)
notify(`Something went wrong: ${error.toString()}`);
}
}

Expand Down

0 comments on commit db92cd5

Please sign in to comment.