PuppeteerでScrapbox操作したい
モチベーション
2021年から日報を復活させようとしてる
日付が切り替わったら今日のlogページが自動的に生成されていてほしい
そうだPuppeteer使おうぜ
参考文献
https://gist.github.com/Brandawg93/728a93e84ed7b66d8dd0af966cb20ecb
https://qiita.com/vicugna-pacos/items/a52e22d08856d1041316#ログインが必要なページへのアクセス
npm i -y
npm i puppeteer
code:index.js
const puppeteer = require("puppeteer");
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800 });
await page.goto("https://scrapbox.io/login");
const navigationPromise = page.waitForNavigation();
await page.waitForSelector("a.btn.btn-default.btn-lg");
await page.click("a.btn.btn-default.btn-lg");
await navigationPromise;
await page.waitForSelector('inputtype="email"');
await page.type('inputtype="email"', 'hoge@gmail.com');
await page.click("#identifierNext");
await page.waitForSelector('inputtype="password"', { visible: true });
await page.type('inputtype="password"', 'password');
await page.waitForSelector("#passwordNext", { visible: true });
await page.click("#passwordNext");
await navigationPromise;
await browser.close();
})();
イメージとしてはこんな感じっぽい
node --unhandled-rejections=strict index.js
--unhandled-rejections=strictの怒られが発生する
https://azu.github.io/slide/error-handling/promise-error-handling.html
headlessがtrueかfalseかでログイン画面が違う
https://gyazo.com/0e151d28b382faf5f5cea0a978db2f63
headless: false
https://gyazo.com/84effb588094472bf0145d20cfe9b7b5
headless: true