Skip to content

Instantly share code, notes, and snippets.

@FujiHaruka
Created September 2, 2017 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FujiHaruka/eede49f4bda6892132772daae2848aec to your computer and use it in GitHub Desktop.
Save FujiHaruka/eede49f4bda6892132772daae2848aec to your computer and use it in GitHub Desktop.
Headless Chrome example for searching on google
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