Created
September 2, 2017 06:13
-
-
Save FujiHaruka/eede49f4bda6892132772daae2848aec to your computer and use it in GitHub Desktop.
Headless Chrome example for searching on google
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Chromy = require('chromy') | |
const word = 'ヘッドレスブラウザ' | |
const chromy = new Chromy() | |
chromy.chain() | |
.goto('https://google.com') | |
.insert('input[type=text]', word) | |
.click('input[value^=Google]', {waitLoadEvent: true}) | |
.evaluate(() => { | |
// Google 検索結果のサイトタイトルは h3 の子要素 a になっている | |
const linkDOMs = document.querySelectorAll('h3 > a') | |
return Array.from(linkDOMs).map( | |
(link) => ({title: link.innerText, url: link.href}) | |
) | |
}) | |
.result((r) => console.log(r)) | |
.end() | |
.catch((e) => { | |
console.log() | |
}) | |
.then(() => chromy.close()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment