Chromeless | 让 Chrome 自动化变得简单

简评:之前,Chrome 已经提供了无界面模式(Chrome Headless)。Chromeless 这个项目又对这个做了一层封装,可以直接远程操控 AWS Lambda 上的 Chrome 了。

使用 Chromeless,开发者可以通过 elegant API 来控制 Chrome(打开网站、点击元素、填写表单等……),这对于集成测试和爬虫都更方便了 ~

测试地址:demo playground

Chromeless 功能

  • 并行执行 1000 次浏览器集成测试⚡️
  • 抓取网页并自动截图
  • 编写需要真实浏览器环境的机器人

示例

JSON of Google Results

const { Chromeless } = require('chromeless')

async function run() {
  const chromeless = new Chromeless({ remote: true })

  const links = await chromeless
    .goto('https://www.google.com')
    .type('chromeless', 'input[name="q"]')
    .press(13)
    .wait('#resultStats')
    .evaluate(() => {
      // this will be executed in headless chrome
      const links = [].map.call(
        document.querySelectorAll('.g h3 a'),
        a => ({title: a.innerText, href: a.href})
      )
      return JSON.stringify(links)
    })
    // you can still use the method chaining API after evaluating
    // when you're done, at any time you can call `.then` (in our case `await`)
    .scrollTo(0, 1000)

  console.log(links)

  await chromeless.end()
}

run().catch(console.error.bind(console))

Google Results 截图

const { Chromeless } = require('chromeless')

async function run() {
  const chromeless = new Chromeless()

  const screenshot = await chromeless
    .goto('https://www.google.com')
    .type('chromeless', 'input[name="q"]')
    .press(13)
    .wait('#resultStats')
    .screenshot()

  console.log(screenshot) // prints local file path or S3 url

  await chromeless.end()
}

run().catch(console.error.bind(console))

更多示例:examples list

运行

有两种方法使用 Chromeless

  • 在本地计算机运行 Chrome
  • 在 AWS Lambda 运行 Chrome 并远程控制

Github:graphcool/chromeless

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容