chrome-automator
基于 chrome-remote-interface 的自动化工具。
API与 Nightmare 保持高度兼容。
Examples: (测试前请确保chrome版本大于60)
chrome({ show: true })
.viewport(1900, 1000)
.useragent('Mozilla/5.0 Chrome/59.0.3071.115 Mobile Safari/537.36')
.goto('https://www.baidu.com/')
.wait('body')
.insert('input#kw', 'hello world\r')
.wait('.c-container a')
.click('.c-container a')
.wait('[id^="highlighter_"]')
.screenshot('1.png')
.evaluate(() => document.querySelectorAll('.para-title.level-3')[9].nextElementSibling.querySelector('.code').textContent)
.pipe((code) => console.log(code))
.url()
.end()
.then((url) => console.log(url))
var Nightmare = require('chrome-automator')
Nightmare.action('hello', function () {
console.log('Get url')
return this.evaluate_now(function () {
return document.querySelector('#links_wrapper a.result__a').href
})
})
var nightmare = Nightmare({ show: true })
try {
nightmare
.goto('https://duckduckgo.com')
.type('#search_form_input_homepage', 'github nightmare')
.click('#search_button_homepage')
.wait('#zero_click_wrapper .c-info__title a')
.hello()
.end()
.then(function (result) {
console.log(result)
})
} catch (e) {
console.log(e)
}
命令行模式 (version >= 0.2.0)
> chrome-automator
.chrome() // 启动chrome
.goto('https://baidu.com')
.goto{ frameId: '64607.1' }
.insert('#kw', 'hello world\r')
.screenshot('1.jpg')
.title()
.end() // 关闭chrome
输出: hello world_百度搜索
API兼容列表如下:
列表如下: 详细说明见 https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-loadingFailed
两种模式:
拓展功能及API:
PS:目前原有框架(Nightmare)回调写法全部去除,仅保留 Promise 写法。
Tips: 执行过程中手动进行某些操作(如打开开发者工具)可能会使用动作失效。
因为 Promise 无法取消的原因,所以在流程执行完 end 操作后node可能并不会立即退出,一般会在 30s 左右自动退出,可以缩短 loadTimeout 和 executionTimeout 解决
Promise 异步流程目前在node下还无法显示完整的错误堆栈信息,可以考虑使用 node --trace-warnings
查看,也可以使用 global.Promise = require('bluebird')
解决,使用过程中记得使用 try catch 包裹执行段
LICENSE
MIT
感谢
- Nightmare
- chrome-remote-interface
- lighthouse